将DataGridView绑定到DataBase

时间:2013-01-24 09:56:49

标签: datagridview ado.net

导入System.Data.SqlClient

Public Class Form1     Dim cn As SqlConnection     Dim ds As DataSet     Dim adapter As SqlDataAdapter     Dim eleveBindingSource作为BindingSource     Dim builder As 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

结束班

1 个答案:

答案 0 :(得分:0)

问题是当我修改UpdateCommand

时,我没有初始化DataGridView

所以我必须在更新DataSet之前添加此代码:

builder = New SqlCommandBuilder(adapter)