这有效:
DataGridView.Rows.Add("Bla Bla Bla", value1, value2)
但如果我这样做:
Dim newrow As New DataGridViewRow
newrow.SetValues("Bla Bla Bla", value1, value2)
If Not value1.Equals(value2) Then
newrow.DefaultCellStyle.ForeColor = Color.Red
End If
DataGridView.Rows.Add(newrow)
整行都是空的。
为什么该行充满空单元格?
最佳答案
您的 newrow
变量没有任何单元格,并且 SetValues
忽略您的信息,因为没有任何可设置值的单元格。
来自 DataGridViewRow.SetValues Method :
If there are more values in the values list than there are cells to be initialized, this method ignores the extra values and returns false. This method also returns false if any of the specified values cannot be set.
If there are fewer values than there are cells, the remaining unmatched cells retain their current values.
使用
CreateCells
填充单元格的方法:newrow.CreateCells(DataGridView)
'' or
newrow.CreateCells(DataGridView, New Object() {"Bla Bla Bla", value1, value2})
关于vb.net - 手动添加 DataGridViewRow 导致空单元格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8761387/