簡體   English   中英

VB.NET DataGridView行

[英]VB.NET DataGridView rows

NET嗨,我是VB NET的新手,我正在嘗試檢查datagridview的同一行中的所有值是否相同。 范例:

For Each row In DataGridView1.Rows
       If 'all values in row are not the same' Then
           row.DefaultCellStyle.BackColor = Color.Red
       End if
Next

告訴我您的問題中是否有您不理解的內容^^

預先感謝您的幫助! :P

如果使用.RowPrepaint處理程序可能會更好

private sub datagridivew_RowPrepaint(sender as Object, e as DataGridViewRowPrePaintEventArgs) Handles datagridview.RowPrePaint
    if e.RowIndex >= 0 then
        Dim dgvr as DataGridViewRow = DirectCast(sender, DataGridView).Rows(e.RowIndex)
        if IsAllValuesAreSame(dgvr) = True Then
            dgvr.DefaultCellStyle.BackColor = Color.Red    
        End If
    End If
End Sub

然后,您無需在所有行初始化之后再循環所有行。
和用於檢查row的所有值的函數:

private function IsAllValuesAreSame(dgvr as DataGridViewRow) as Boolean
    Dim distinctQnt As Int32 = (From cell As DataGridViewCell In dgvr.Cells
                                Where cell.ColumnIndex >= 0
                                Select cell.Value).Distinct().Count()
    Return (distinctQnt = 1)
End Function

您需要將每行中所有單元格的值與該行中的一個單元格進行匹配,以了解該行中的所有單元格是否具有相同的值。 下面,我為您提供了最簡單的方法,並且選擇每行第一列的值來驗證該行中其他列的值是否等於它。 如果某行的所有列/單元格都不具有相同的值,則該行的背景色將變為紅色。

    For x = 0 To DataGridView1.RowCount - 1
        For y = 0 To DataGridView1.ColumnCount - 1
            If DataGridView1.Item(y, x).Value = DataGridView1.Item(0, x).Value Then
                'yes
            Else
                'no
                 DataGridView1.Rows.Item(x).DefaultCellStyle.BackColor = Color.Red
            End If
        Next
    Next

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM