Sikret Miseon
Sikret Miseon

Reputation: 557

how to update database using datagridview?

how do we update tables using datagridview? assuming the datagridview is editable at runtime? any kind of help is appreciated.

 Dim con As New OleDb.OleDbConnection
    Dim dbProvider As String
    Dim dbSource As String
    Dim ds As New DataSet
    Dim da As OleDb.OleDbDataAdapter
    Dim sql As String

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        dbProvider = "PROVIDER=Microsoft.ACE.OLEDB.12.0;"
        dbSource = "Data Source = C:\record.accdb"

        con.ConnectionString = dbProvider & dbSource

        con.Open()

        sql = "SELECT * FROM StudentRecords"
        da = New OleDb.OleDbDataAdapter(sql, con)
        da.Fill(ds, "AddressBook")
        MsgBox("Database is now open")

        con.Close()

        MsgBox("Database is now Closed")

        DataGridView1.DataSource = ds
        DataGridView1.DataMember = "AddressBook"
    End Sub

Upvotes: 0

Views: 1281

Answers (1)

KV Prajapati
KV Prajapati

Reputation: 94653

Please follow these steps:

  1. Iterate the Rows collection of DataGridView using For Each or simple For loop
  2. Write UPDATE statement to save changes.

Upvotes: 2

Related Questions