I work with windows forms and on the form I have a DataGridView. This gridView I fill with a DataTable dt. The dt I fill with OdbcDataAdapter. For save the changes I use adaper.Update(dt) on Event Handler gridView_RowLeave. The problem is that dt doesn’t have last changes of gridView. For exemple if I modify some data in one row or add a new row with data when I leave that row the method dt.GetChages() return false and I must leave more that 1 row for dt.GetChanges() return true. Where can be the problem? Here is my code:
Private Sub FillGridview()
Dim adapter As OdbcDataAdapter
Dim sCommand As New OdbcCommand("Select * from SPEAKERS", con)
adapter = New OdbcDataAdapter(sCommand)
adapter.UpdateCommand = UpdateCommand() 'return a query for Update
adapter.InsertCommand = InsertCommand() 'return a query for Insert but without ID
adapter.DeleteCommand = DeleteCommand()
Try
adapter.Fill(dt)
Catch ex As Exception
MsgBox(ex.Message)
End Try
gridView.DataSource = dt
End Sub
Private Sub gridView_RowLeave(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles gridView.RowLeave
gridView.EndEdit()
If dt.GetChanges() IsNot Nothing Then
Try
Dim res As Integer = adapter.Update(dt)
Catch ex As Exception
MsgBox(ex.Message)
dt.Clear()
adapter.Fill(dt)
End Try
If Not dt.HasErrors Then
dt.AcceptChanges()
End If
gridView.Refresh()
End If
End Sub
Does anyone have any idea why dt don’t have last changes?
Thanks in advance!
Try to put a bindingSource component to handle data and do not link the grid directly to data