2013-01-24 106 views
0

進口System.Data.SqlClient的一個DataGridView綁定到數據庫

公共類Form1中 昏暗CN作爲SqlConnection的 昏暗DS作爲數據集 昏暗適配器作爲SqlDataAdapter的 昏暗eleveBindingSource作爲BindingSource的 昏暗助洗劑如SqlCommandBuilder

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load 
    cn = New SqlConnection("Data source=.; initial catalog=tp4_ado; integrated security=sspi") 
    ds = New DataSet() 
    eleveBindingSource = New BindingSource 
    adapter = New SqlDataAdapter("Select * from Eleve", cn) 
    adapter.Fill(ds, "Eleve") 
    eleveBindingSource.DataSource = ds 
    eleveBindingSource.DataMember = "Eleve" 
    DataGridView1.DataSource = eleveBindingSource 
End Sub 

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click 
    adapter.Update(ds.Tables("Eleve")) 
End Sub 

結束等級

+0

你嘗試過什麼?如果我們看到您嘗試過的東西,我們可以告訴您爲什麼它不起作用或者有更好的方法。 –

+0

@TimSchmelter okey,我修改了我的帖子 –

+0

好吧,我解決了我的問題 –

回答

0

問題是我沒有初始化UpdateCommand當我做了修改的DataGridView

所以我必須在更新前DataSet添加此代碼:

builder = New SqlCommandBuilder(adapter)