2013-05-27 53 views
0

在我的VB類新行我已經定義了這個屬性爲類變量:裏面插入的DataGridView

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

你的DataGrid綁定到數據源,這樣,那麼你應該在數據源

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 
+2

我敢肯定,@ marianoc84已經做到了.. – matzone

+0

沒有被添加到表中添加行:| –

+0

@ marianoc84:它已更改..再試一次 – matzone