这有效: DataGridView.Rows.Add("Bla Bla Bla", value1, value2) 但如果我这样做: Dim newrow As New DataGridViewRownewrow.SetValues("Bla Bla Bla", value1, value2)If Not value1.Equals(value2) Then newrow.DefaultCellStyle.ForeColor =
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})