user3106114
user3106114

Reputation: 183

Saving to different table from DataGridView in VB.net

I am working on VB.net Windows application. I have a DataGridView that is loading from different three tables. My grid view looks like this:

 **Make    model        color**

toyota    corolla    red

Bmw       c100      white

My three table are named Make_tbl, Model_tbl,Color_tbl. In my windows form I have edit and save buttons

After editing anything I want to save my data. While saving the same time i want to save this data to different three tables. How i can do this VB.net

Upvotes: 0

Views: 2166

Answers (1)

ɐsɹǝʌ ǝɔıʌ
ɐsɹǝʌ ǝɔıʌ

Reputation: 4512

This code takes a MS Access DB and update data (Insert/Update/Delete) back into database from DataGridView on Button1_Click

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 

Upvotes: 1

Related Questions