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

标签 vb.net datagridview

这有效:

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/

相关文章:

c# - 自定义图标会破坏 DataGridView?

c# - 如何使用 CellValueChanged 事件检索 DataGridView 单元格的先前值?

c# - 如何将数据从 texboxes 插入到 datagridview?

c# - 从 Android 中的 ASPX 页面获取响应

TimePicker : Enter Key Does not Update Displayed Value 的 DataGridView 单元格中的 VB.NET 更新问题

.net - 我们如何向 COM 互操作公开 .NET 公共(public)常量

vb.net - 如何使用所有打开表单的列表填充列表框

c# - 如何在不按 Tab 的情况下强制 DataGridView 当前单元格轮廓

c# - 覆盖 ServicePointManager.ServerCertificateValidationCallback 时执行默认证书验证

vb.net - 如何增加子报表的内容?