Question problem with DataTable.GetChanges function

pooya1072

Well-known member
Joined
Jul 5, 2012
Messages
84
Programming Experience
Beginner
hi friends
i have a datagridview in my form . it has a datatable as a datasource named "dt". after any changes :
if i put getChanges function in a button click event getchanges function return nothing
but in the formClosing event it return any change as a datatable
look this commands :
VB.NET:
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
   Dim [COLOR=#ff0000]Changes[/COLOR] As DataTable = dt.GetChanges
End Sub
the Changes has nothing
VB.NET:
Private Sub Form1_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
   Dim [COLOR=#ff0000]Changes[/COLOR] As DataTable = dt.GetChanges
End Sub
the changes has all changes.
why in the button click event return nothing?
 
If you have bound a DataTable to a DataGridView and edit a row in the grid, the changes you make are not immediately pushed down to the DataTable. There is an underlying CurrencyManager and its EndCurrentEdit method must be called to commit the change. That will occur implicitly if you navigate to another row in the grid or, presumably, if you close the form. I suspect that that method hasn't been called when you are clicking that Button. Try navigating to a different row and see if you still get Nothing. Generally speaking, what you should be doing is binding your DataTable to a BindingSource and binding that to the grid. You can then call EndEdit on the BindingSource to commit and pending changes before saving.
 
Back
Top