在我的VB类中,我将此属性定义为类变量:
Friend WithEvents dgw As System.Windows.Forms.DataGridView
在同一个类中的某个地方定义了一个属性:
Private Sub mySub(...)
Dim aDataAdapter As SqlDataAdapter
Dim aDataSet = New DataSet
aDataAdapter = New SqlDataAdapter("exec something")
aDataAdapter.Fill(aDataSet)
Me.dgw.DataSource = aDataSet.Tables(0)
' [PLACEHOLDER]
End Sub
此代码完成其工作:数据从数据库中读取并插入网格中。现在我将在该网格中手动添加新行,因此我编写了此代码来代替[PLACEHOLDER]
:
Dim emptyRow As DataGridViewRow
emptyRow.SetValues(New String() {"a", "b", "c", "d", "e"})
Me.dgw.Rows.Insert(2, emptyRow)
但DataGridView
没有发生任何事情(即没有插入行)。怎么了?
答案 0 :(得分:0)
您的数据网格绑定到数据源,因此您应该在该数据源中添加行
Dim drCopy as DataRow
Dim tbl as DataTable = aDataSet.Tables(0)
drCopy=tbl.NewRow()
drCopy.item(0)="a"
drCopy.item(1)="b"
drCopy.item(2)="c"
drCopy.item(3)="d"
drCopy.item(4)="e"
tbl.Rows.Add(drCopy)
Me.dgw.datasource = tbl