[英]VB.NET DataGridView rows
NET Hi I'm new in VB NET and I'm trying to check if all values in the same row of a datagridview are the same. NET嗨,我是VB NET的新手,我正在尝试检查datagridview的同一行中的所有值是否相同。 Exemple :
范例:
For Each row In DataGridView1.Rows
If 'all values in row are not the same' Then
row.DefaultCellStyle.BackColor = Color.Red
End if
Next
Tell me if there is anything that you don't understand in my question ^^ 告诉我您的问题中是否有您不理解的内容^^
Thank you in advance for your help! 预先感谢您的帮助! :P
:P
May be better if you use handler of .RowPrepaint
如果使用
.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
Then you don't need to loop all rows one more time after all rows initialized. 然后,您无需在所有行初始化之后再循环所有行。
And function for checking all values of row: 和用于检查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
You need to match the value of all the cells in each row with one cell in that row to know if all cells in that row have the same value. 您需要将每行中所有单元格的值与该行中的一个单元格进行匹配,以了解该行中的所有单元格是否具有相同的值。 Below, I'm giving you the simplest method to do it and I am choosing the value of the first column of each row to verify if the values of other columns in that row are equal to it.
下面,我为您提供了最简单的方法,并且选择每行第一列的值来验证该行中其他列的值是否等于它。 If all the columns/cells of a row don't have the same value, then that row's back color will turn red.
如果某行的所有列/单元格都不具有相同的值,则该行的背景色将变为红色。
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.