VB.NET DataGridView行
NET嗨,我是VB NET的新手,我正在尝试检查datagridview的同一行中的所有值是否相同。 例如:VB.NET DataGridView行,vb.net,datagridview,equals,rows,Vb.net,Datagridview,Equals,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
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
这样,在所有行初始化之后,就不需要再次循环所有行了。和用于检查行的所有值的函数:
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
如果我理解,您可能需要再次循环它,以将每一行与其他行匹配为红色。对不起,我是VB.NET的初学者,我不理解您的示例,“FunctionisAllValuesAreName”未声明:(您能详细解释一下吗?非常感谢!我编辑了一点代码,它完全符合我的要求!:D