views:

454

answers:

2

Here is my code, but its the other way around, it commits changes to the datagridview when the database is updated.

Imports System    
Imports System.Data    
Imports System.Data.OleDb    
Imports System.Windows.Forms

Public Class Form1    
Inherits System.Windows.Forms.Form

    Private bindingSource1 As New BindingSource()    
    Private dataAdapter As New OleDbDataAdapter()

    <STAThreadAttribute()> _    
    Public Shared Sub Main()    
        Application.Run(New Form1())    
    End Sub

    Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load    
        Dim connectionString As String = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\GRN.mdb;User Id=admin;Password=;"    
        Dim selectCommand As String    
        Dim connection As New OleDbConnection(connectionString)

        selectCommand = "select * from GRNITEMS ORDER BY GRNNO, ITEMCODE"    
        Me.dataAdapter = New OleDbDataAdapter(selectCommand, connection)

        With DataGridView1    
            .AutoSizeRowsMode = DataGridViewAutoSizeRowsMode.AllCells    
            .AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.ColumnHeader    
            .AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.ColumnHeader    
        End With

        Dim commandBuilder As New OleDbCommandBuilder(Me.dataAdapter)    
        Dim table As New DataTable()

        table.Locale = System.Globalization.CultureInfo.InvariantCulture

        Me.dataAdapter.Fill(table)    
        Me.bindingSource1.DataSource = table

        Dim data As New DataSet()

        data.Locale = System.Globalization.CultureInfo.InvariantCulture

        DataGridView1.DataSource = Me.bindingSource1    
        Me.DataGridView1.AlternatingRowsDefaultCellStyle.BackColor = Color.Aqua    
        Me.DataGridView1.AutoResizeColumns( _    
            DataGridViewAutoSizeColumnsMode.AllCells)

    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim table As New DataTable()

        Me.bindingSource1 = Me.DataGridView1.DataSource    
        table = Me.bindingSource1.DataSource

        Me.dataAdapter.Update(table)    
    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        Me.Close()   
    End Sub

End Class
A: 

I suppose this code belongs to this question?

stakx
A: 

this line:

Me.dataAdapter.Update(table)

updates your table with recent data from your database using your dataAdapter.

That's where the problem is. I'm not familiar enough with VB.NET to write you the code to update your database with your new data from gridview.

Tony
-1 This is not a complete answer.. you have to specify the UpdateCommand.
0A0D