I have a datagrid on a form that update properly when form loads. When the program runs and I type in/add a new row in the datagrid, it does not save the changes. Any help would be greatly appreaciated. Thanks.
Code below:
Imports System.Data.OleDb
Public Class Form2
Inherits System.Windows.Forms.Form
#Region " Windows Form Designer generated code "
Public Sub New()
MyBase.New()
'This call is required by the Windows Form Designer.
InitializeComponent()
'Add any initialization after the InitializeComponent() call
End Sub
'Form overrides dispose to clean up the component list.
Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
If disposing Then
If Not (components Is Nothing) Then
components.Dispose()
End If
End If
MyBase.Dispose(disposing)
End Sub
'Required by the Windows Form Designer
Private components As System.ComponentModel.IContainer
'NOTE: The following procedure is required by the Windows Form Designer
'It can be modified using the Windows Form Designer.
'Do not modify it using the code editor.
Friend WithEvents butExit As System.Windows.Forms.Button
Friend WithEvents butSave As System.Windows.Forms.Button
Friend WithEvents DataGrid1 As System.Windows.Forms.DataGrid
<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
Me.butExit = New System.Windows.Forms.Button
Me.DataGrid1 = New System.Windows.Forms.DataGrid
Me.butSave = New System.Windows.Forms.Button
CType(Me.DataGrid1, System.ComponentModel.ISupportInitialize).BeginIni t()
Me.SuspendLayout()
'
'butExit
'
Me.butExit.Location = New System.Drawing.Point(600, 312)
Me.butExit.Name = "butExit"
Me.butExit.Size = New System.Drawing.Size(160, 24)
Me.butExit.TabIndex = 8
Me.butExit.Text = "Exit"
'
'DataGrid1
'
Me.DataGrid1.CaptionText = "Employee Information"
Me.DataGrid1.DataMember = ""
Me.DataGrid1.Dock = System.Windows.Forms.DockStyle.Top
Me.DataGrid1.HeaderForeColor = System.Drawing.SystemColors.ControlText
Me.DataGrid1.Location = New System.Drawing.Point(0, 0)
Me.DataGrid1.Name = "DataGrid1"
Me.DataGrid1.Size = New System.Drawing.Size(784, 232)
Me.DataGrid1.TabIndex = 9
'
'butSave
'
Me.butSave.Location = New System.Drawing.Point(600, 272)
Me.butSave.Name = "butSave"
Me.butSave.Size = New System.Drawing.Size(160, 24)
Me.butSave.TabIndex = 10
Me.butSave.Text = "Save Changes"
'
'Form2
'
Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
Me.ClientSize = New System.Drawing.Size(784, 374)
Me.Controls.Add(Me.butSave)
Me.Controls.Add(Me.DataGrid1)
Me.Controls.Add(Me.butExit)
Me.Name = "Form2"
Me.Text = "Form2"
CType(Me.DataGrid1, System.ComponentModel.ISupportInitialize).EndInit( )
Me.ResumeLayout(False)
End Sub
#End Region
Private m_DataAdapter As OleDbDataAdapter
Private m_DataSet As New DataSet
Private Sub Form2_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Load
m_DataAdapter = New OleDbDataAdapter( _
"SELECT * FROM Emp", _
"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\PR\Payroll.mdb")
' Map the default name Table to Books.
m_DataAdapter.TableMappings.Add("Table", "Emp")
' Fill the DataSet.
m_DataAdapter.Fill(m_DataSet)
' Bind the DataGrid to the DataSet's first DataTable.
DataGrid1.DataSource = m_DataSet.Tables(0)
' Give the columns sane widths.
Dim table_style As New DataGridTableStyle
table_style.MappingName = "Emp"
DataGrid1.TableStyles.Add(table_style)
table_style.GridColumnStyles(0).Width = 200
table_style.GridColumnStyles(1).Width = 200
End Sub
Private Sub butExit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles butExit.Click
Me.Close()
End Sub
Private Sub Form2_Closing(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles MyBase.Closing
' Make a command builder to generate INSERT,
' UPDATE, and DELETE commands as necessary.
Dim command_builder As New OleDbCommandBuilder(m_DataAdapter)
' Save any changes.
m_DataAdapter.Update(m_DataSet)
End Sub
End Class