从VB.net中的DataGridView保存到不同的表

时间:2013-12-19 07:08:02

标签: vb.net gridview datagridview

我正在使用VB.net Windows应用程序。我有一个从不同的三个表加载的DataGridView。我的网格视图如下所示:

 **Make    model        color**

toyota    corolla    red

Bmw       c100      white

我的三个表名为Make_tblModel_tblColor_tbl。在我的Windows窗体中,我有editsave按钮

编辑任何内容后,我想保存我的数据。在保存的同时我想将这些数据保存到不同的三个表中。我怎么做VB.net

1 个答案:

答案 0 :(得分:1)

此代码从DataGridView

上的Button1_Click获取MS Access数据库并将数据(插入/更新/删除)更新回数据库
Imports System.Data.OleDb  

Public Class Form1  

    Dim myDA As OleDbDataAdapter  
    Dim myDataSet As DataSet  

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load  
        Dim con As OleDbConnection = New OleDbConnection("Provider=Microsoft.jet.oledb.4.0;data source=|DataDirectory|\myDB.mdb")  
        Dim cmd As OleDbCommand = New OleDbCommand("SELECT * FROM Table1", con)  
        con.Open()  
        myDA = New OleDbDataAdapter(cmd)  

        'One CommandBuilder object is required. It automatically generates DeleteCommand,UpdateCommand and InsertCommand for DataAdapter object  
        Dim builder As OleDbCommandBuilder = New OleDbCommandBuilder(myDA)  
        myDataSet = New DataSet()  
        myDA.Fill(myDataSet, "MyTable")
        myDA.UpdateCommand = new OledbCommandBuilder(myDA).GetUpdateCommand();  
        DataGridView1.DataSource = myDataSet.Tables("MyTable") 
        con.Close()  
        con = Nothing 
     End Sub 

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click  
        Me.Validate()  
        Me.myDA.Update(Me.myDataSet.Tables("MyTable"))  
        Me.myDataSet.AcceptChanges()  
    End Sub 

End Class