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.Ole Db
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.
InitializeCompo nent()
'Add any initialization after the InitializeCompo nent() 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.Disp ose()
End If
End If
MyBase.Dispose( disposing)
End Sub
'Required by the Windows Form Designer
Private components As System.Componen tModel.IContain er
'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.Diagnos tics.DebuggerSt epThrough()> Private Sub InitializeCompo nent()
Me.butExit = New System.Windows. Forms.Button
Me.DataGrid1 = New System.Windows. Forms.DataGrid
Me.butSave = New System.Windows. Forms.Button
CType(Me.DataGr id1, System.Componen tModel.ISupport Initialize).Beg inInit()
Me.SuspendLayou t()
'
'butExit
'
Me.butExit.Loca tion = New System.Drawing. Point(600, 312)
Me.butExit.Name = "butExit"
Me.butExit.Size = New System.Drawing. Size(160, 24)
Me.butExit.TabI ndex = 8
Me.butExit.Text = "Exit"
'
'DataGrid1
'
Me.DataGrid1.Ca ptionText = "Employee Information"
Me.DataGrid1.Da taMember = ""
Me.DataGrid1.Do ck = System.Windows. Forms.DockStyle .Top
Me.DataGrid1.He aderForeColor = System.Drawing. SystemColors.Co ntrolText
Me.DataGrid1.Lo cation = New System.Drawing. Point(0, 0)
Me.DataGrid1.Na me = "DataGrid1"
Me.DataGrid1.Si ze = New System.Drawing. Size(784, 232)
Me.DataGrid1.Ta bIndex = 9
'
'butSave
'
Me.butSave.Loca tion = New System.Drawing. Point(600, 272)
Me.butSave.Name = "butSave"
Me.butSave.Size = New System.Drawing. Size(160, 24)
Me.butSave.TabI ndex = 10
Me.butSave.Text = "Save Changes"
'
'Form2
'
Me.AutoScaleBas eSize = 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.DataGr id1, System.Componen tModel.ISupport Initialize).End Init()
Me.ResumeLayout (False)
End Sub
#End Region
Private m_DataAdapter As OleDbDataAdapte r
Private m_DataSet As New DataSet
Private Sub Form2_Load(ByVa l sender As Object, ByVal e As System.EventArg s) Handles MyBase.Load
m_DataAdapter = New OleDbDataAdapte r( _
"SELECT * FROM Emp", _
"Provider=Micro soft.Jet.OLEDB. 4.0;Data Source=C:\PR\Pa yroll.mdb")
' Map the default name Table to Books.
m_DataAdapter.T ableMappings.Ad d("Table", "Emp")
' Fill the DataSet.
m_DataAdapter.F ill(m_DataSet)
' Bind the DataGrid to the DataSet's first DataTable.
DataGrid1.DataS ource = m_DataSet.Table s(0)
' Give the columns sane widths.
Dim table_style As New DataGridTableSt yle
table_style.Map pingName = "Emp"
DataGrid1.Table Styles.Add(tabl e_style)
table_style.Gri dColumnStyles(0 ).Width = 200
table_style.Gri dColumnStyles(1 ).Width = 200
End Sub
Private Sub butExit_Click(B yVal sender As System.Object, ByVal e As System.EventArg s) Handles butExit.Click
Me.Close()
End Sub
Private Sub Form2_Closing(B yVal sender As Object, ByVal e As System.Componen tModel.CancelEv entArgs) Handles MyBase.Closing
' Make a command builder to generate INSERT,
' UPDATE, and DELETE commands as necessary.
Dim command_builder As New OleDbCommandBui lder(m_DataAdap ter)
' Save any changes.
m_DataAdapter.U pdate(m_DataSet )
End Sub
End Class
Code below:
Imports System.Data.Ole Db
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.
InitializeCompo nent()
'Add any initialization after the InitializeCompo nent() 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.Disp ose()
End If
End If
MyBase.Dispose( disposing)
End Sub
'Required by the Windows Form Designer
Private components As System.Componen tModel.IContain er
'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.Diagnos tics.DebuggerSt epThrough()> Private Sub InitializeCompo nent()
Me.butExit = New System.Windows. Forms.Button
Me.DataGrid1 = New System.Windows. Forms.DataGrid
Me.butSave = New System.Windows. Forms.Button
CType(Me.DataGr id1, System.Componen tModel.ISupport Initialize).Beg inInit()
Me.SuspendLayou t()
'
'butExit
'
Me.butExit.Loca tion = New System.Drawing. Point(600, 312)
Me.butExit.Name = "butExit"
Me.butExit.Size = New System.Drawing. Size(160, 24)
Me.butExit.TabI ndex = 8
Me.butExit.Text = "Exit"
'
'DataGrid1
'
Me.DataGrid1.Ca ptionText = "Employee Information"
Me.DataGrid1.Da taMember = ""
Me.DataGrid1.Do ck = System.Windows. Forms.DockStyle .Top
Me.DataGrid1.He aderForeColor = System.Drawing. SystemColors.Co ntrolText
Me.DataGrid1.Lo cation = New System.Drawing. Point(0, 0)
Me.DataGrid1.Na me = "DataGrid1"
Me.DataGrid1.Si ze = New System.Drawing. Size(784, 232)
Me.DataGrid1.Ta bIndex = 9
'
'butSave
'
Me.butSave.Loca tion = New System.Drawing. Point(600, 272)
Me.butSave.Name = "butSave"
Me.butSave.Size = New System.Drawing. Size(160, 24)
Me.butSave.TabI ndex = 10
Me.butSave.Text = "Save Changes"
'
'Form2
'
Me.AutoScaleBas eSize = 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.DataGr id1, System.Componen tModel.ISupport Initialize).End Init()
Me.ResumeLayout (False)
End Sub
#End Region
Private m_DataAdapter As OleDbDataAdapte r
Private m_DataSet As New DataSet
Private Sub Form2_Load(ByVa l sender As Object, ByVal e As System.EventArg s) Handles MyBase.Load
m_DataAdapter = New OleDbDataAdapte r( _
"SELECT * FROM Emp", _
"Provider=Micro soft.Jet.OLEDB. 4.0;Data Source=C:\PR\Pa yroll.mdb")
' Map the default name Table to Books.
m_DataAdapter.T ableMappings.Ad d("Table", "Emp")
' Fill the DataSet.
m_DataAdapter.F ill(m_DataSet)
' Bind the DataGrid to the DataSet's first DataTable.
DataGrid1.DataS ource = m_DataSet.Table s(0)
' Give the columns sane widths.
Dim table_style As New DataGridTableSt yle
table_style.Map pingName = "Emp"
DataGrid1.Table Styles.Add(tabl e_style)
table_style.Gri dColumnStyles(0 ).Width = 200
table_style.Gri dColumnStyles(1 ).Width = 200
End Sub
Private Sub butExit_Click(B yVal sender As System.Object, ByVal e As System.EventArg s) Handles butExit.Click
Me.Close()
End Sub
Private Sub Form2_Closing(B yVal sender As Object, ByVal e As System.Componen tModel.CancelEv entArgs) Handles MyBase.Closing
' Make a command builder to generate INSERT,
' UPDATE, and DELETE commands as necessary.
Dim command_builder As New OleDbCommandBui lder(m_DataAdap ter)
' Save any changes.
m_DataAdapter.U pdate(m_DataSet )
End Sub
End Class