0

在我的 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(即没有插入行)。怎么了?

4

1 回答 1

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
于 2013-05-27T16:13:39.113 回答