当前位置 : 主页 > 编程语言 > c语言 >

vb.net – 手动添加DataGridViewRow会导致空单元格

来源:互联网 收集:自由互联 发布时间:2021-06-24
这有效: 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})
网友评论