加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

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

发布时间:2020-12-17 07:15:13 所属栏目:百科 来源:网络整理
导读:这有效: DataGridView.Rows.Add("Bla Bla Bla",value1,value2) 但如果我这样做: Dim newrow As New DataGridViewRownewrow.SetValues("Bla Bla Bla",value2)If Not value1.Equals(value2) Then newrow.DefaultCellStyle.ForeColor = Color.RedEnd IfDataGr
这有效:

DataGridView.Rows.Add("Bla Bla Bla",value1,value2)

但如果我这样做:

Dim newrow As New DataGridViewRow
newrow.SetValues("Bla Bla Bla",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",value2})

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读