Datagridview + Update







Imports System.Data.OleDb
Public Class Form1
    Dim strconn As String = "Provider=Microsoft.Jet.OLEDB.4.0;" & _
                            "Data Source=c:/dthai97.mdb"
    Dim cn As New OleDbConnection(strconn)
    Dim da As New OleDbDataAdapter
    Dim ds As New DataSet
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        da.SelectCommand = New OleDbCommand("select * from tbthai", cn)
        da.Fill(ds, "tb")
        DataGridView1.DataSource = ds.Tables("tb")
    End Sub
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Try
            Dim cb As New OleDbCommandBuilder(da)
            Dim chg As Integer = da.Update(ds, "tb")
            MsgBox(DataGridView1.Rows.Count())
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
    End Sub
End Class

Imports System.Data.OleDb Public Class Form1 Dim strconn As String = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=c:/dthai97.mdb" Dim cn As New OleDbConnection(strconn) Dim da As New OleDbDataAdapter Dim ds As New DataSet Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load da.SelectCommand = New OleDbCommand("select * from tbthai", cn) da.Fill(ds, "tb") DataGridView1.DataSource = ds.Tables("tb") End Sub Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click Dim cb As New OleDbCommandBuilder(da) Try Dim chg As Integer = da.Update(ds, "tb") MsgBox(chg) 'MsgBox(DataGridView1.Rows.Count()) Catch ex As Exception MsgBox(ex.Message) End Try End Sub Private Sub DataGridView1_CellClick(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.CellClick Dim x As Integer = Val(DataGridView1.CurrentRow.Index.ToString) TextBox1.Text = DataGridView1.Rows(x).Cells(0).Value TextBox2.Text = DataGridView1.Rows(x).Cells(1).Value TextBox3.Text = DataGridView1.Rows(x).Cells(2).Value DataGridView1.Enabled = False End Sub Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click DataGridView1.Enabled = True Dim x As Integer = Val(DataGridView1.CurrentRow.Index.ToString) DataGridView1.Rows(x).Cells(0).Value = TextBox1.Text DataGridView1.Rows(x).Cells(1).Value = TextBox2.Text DataGridView1.Rows(x).Cells(2).Value = TextBox3.Text DataGridView1.CurrentCell = DataGridView1.Item(0, x) DataGridView1.CurrentRow.Selected = True DataGridView1.Focus() End Sub End Class

http://goo.gl/72BPC