G
Guest
How do I update my datagrid to save the modified values to the database table
I opened up NorthwindDemo.sdf in a datagrid, edit a cell using a textbox (using buttonSetCurrentCell_Click). I just lack the knowledge to update any changes I made to the datagrid back to the database table customers. Please help
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Clic
Dim cn As New SqlCeConnectio
Tr
cn = New SqlCeConnection("data source=" & "\Program files\Northwindtest\NorthwindDemo.sdf"
cn.Open(
Dim cmd As SqlCeCommand = cn.CreateComman
cmd.CommandText = "select * from customers
Dim da As New SqlCeDataAdapter(cmd
Dim ds As New DataSe
'Or below as a data tabl
'Dim ds As DataTable = New DataTable("Customers"
da.Fill(ds
DataGrid1.Enabled = Tru
DataGrid1.DataSource = ds.Tables(0
'Catch database error
Catch sqlex As SqlCeExceptio
Dim sqlerror As SqlCeErro
For Each sqlerror In sqlex.Error
MessageBox.Show(sqlerror.Message
Nex
Catch ex As Exceptio
MessageBox.Show(ex.Message
Finall
DataGrid1.Enabled = Tru
'Dim dt As New DataSet("NorthwindDemo"
'Dim db As New SqlCeDataAdapte
'db.Update(dt
If cn.State <> ConnectionState.Closed The
cn.Close(
End I
End Tr
End Su
Private Sub dataGrid1_CurrentCellChanged(ByVal o As Object, ByVal e As EventArgs)
Handles DataGrid1.CurrentCellChange
Dim currentCell As DataGridCel
Dim currentCellData As Strin
' Get the current cell
currentCell = DataGrid1.CurrentCel
If currentCell.ColumnNumber = 0 The
TextBoxCurrentCell.Text = "
Exit Su
Els
' Get the current cell's data
currentCellData = CStr(DataGrid1(currentCell.RowNumber, currentCell.ColumnNumber)
' Set the TextBox's text to that of the current cell
TextBoxCurrentCell.Focus(
' Set the TextBox's text to that of the current cell
If Not currentCellData.ToString = Nothing The
TextBoxCurrentCell.Text = currentCellDat
End I
End I
End Su
Private Sub buttonSetCurrentCell_Click(ByVal o As Object, ByVal e As EventArgs)
Handles buttonSetCurrentCell.Clic
Dim currentCell As DataGridCel
Dim currentCellData As Strin
' Get the text to put into the current cell
currentCellData = TextBoxCurrentCell.Tex
' Get the current cell
currentCell = DataGrid1.CurrentCel
' Set the current cell's data
DataGrid1(currentCell.RowNumber, currentCell.ColumnNumber) = currentCellDat
End Sub
I opened up NorthwindDemo.sdf in a datagrid, edit a cell using a textbox (using buttonSetCurrentCell_Click). I just lack the knowledge to update any changes I made to the datagrid back to the database table customers. Please help
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Clic
Dim cn As New SqlCeConnectio
Tr
cn = New SqlCeConnection("data source=" & "\Program files\Northwindtest\NorthwindDemo.sdf"
cn.Open(
Dim cmd As SqlCeCommand = cn.CreateComman
cmd.CommandText = "select * from customers
Dim da As New SqlCeDataAdapter(cmd
Dim ds As New DataSe
'Or below as a data tabl
'Dim ds As DataTable = New DataTable("Customers"
da.Fill(ds
DataGrid1.Enabled = Tru
DataGrid1.DataSource = ds.Tables(0
'Catch database error
Catch sqlex As SqlCeExceptio
Dim sqlerror As SqlCeErro
For Each sqlerror In sqlex.Error
MessageBox.Show(sqlerror.Message
Nex
Catch ex As Exceptio
MessageBox.Show(ex.Message
Finall
DataGrid1.Enabled = Tru
'Dim dt As New DataSet("NorthwindDemo"
'Dim db As New SqlCeDataAdapte
'db.Update(dt
If cn.State <> ConnectionState.Closed The
cn.Close(
End I
End Tr
End Su
Private Sub dataGrid1_CurrentCellChanged(ByVal o As Object, ByVal e As EventArgs)
Handles DataGrid1.CurrentCellChange
Dim currentCell As DataGridCel
Dim currentCellData As Strin
' Get the current cell
currentCell = DataGrid1.CurrentCel
If currentCell.ColumnNumber = 0 The
TextBoxCurrentCell.Text = "
Exit Su
Els
' Get the current cell's data
currentCellData = CStr(DataGrid1(currentCell.RowNumber, currentCell.ColumnNumber)
' Set the TextBox's text to that of the current cell
TextBoxCurrentCell.Focus(
' Set the TextBox's text to that of the current cell
If Not currentCellData.ToString = Nothing The
TextBoxCurrentCell.Text = currentCellDat
End I
End I
End Su
Private Sub buttonSetCurrentCell_Click(ByVal o As Object, ByVal e As EventArgs)
Handles buttonSetCurrentCell.Clic
Dim currentCell As DataGridCel
Dim currentCellData As Strin
' Get the text to put into the current cell
currentCellData = TextBoxCurrentCell.Tex
' Get the current cell
currentCell = DataGrid1.CurrentCel
' Set the current cell's data
DataGrid1(currentCell.RowNumber, currentCell.ColumnNumber) = currentCellDat
End Sub