Datagrid - recognizing changes made

B

Brian Hoops

I have a windows forms datagrid and I would like to be able to recognize
when changes have been made in order to perform the update. I tried the
following, which works well, but only if the user changes rows at some
point. If they enter the grid, change a cell, and then click close it does
not see the change. The same holds true if they enter the grid, change a
cell, arrow right or left to a different cell and change that value. If
they arrow up or down it sees the dataset as having changes.

What am I missing? I don't want to rely on the user having to press enter
or selecting a different row after making their update.

Thanks!

--------------------Code---------------------------------
Dim blnDataChanged As Boolean = False
Dim dt As DataTable

dt = ds.Tables("tblCIP").GetChanges

If dt Is Nothing Then
Return False
Else
Dim Row As DataRow
Dim intColumn As Integer
For Each Row In dt.Rows
Select Case Row.RowState
Case DataRowState.Added
blnDataChanged = True
Case DataRowState.Deleted
blnDataChanged = True
Case DataRowState.Modified
For intColumn = 0 To dt.Columns.Count - 1
If Not IsDBNull(Row(intColumn, DataRowVersion.Original)) And Not
IsDBNull(Row(intColumn, DataRowVersion.Current)) Then
If Row(intColumn, DataRowVersion.Original) <> Row(intColumn,
DataRowVersion.Current) Then
blnDataChanged = True
Exit For
End If
End If
Next
End Select

If blnDataChanged Then Exit For
Next

Return blnDataChanged
end If
 
B

Brian Hoops

Also another issue along the same lines....

If the datagrid is loaded with no rows, then a new row is added by typing in
the bottom empty 'add new' row, then the row is changed (required - see
below) the code I detailed below WILL see the change made. Then when I
perform an update - odaCIP.Update(DsCIP, "tblCIP") - that new row isn't
added. If the dataset was loaded with multiple existing rows, it will add
the new row added using the same method.

Thanks for any help!
 
V

+Vice

try this before you call the update:
me.bindingcontext(DsCIP,"tblCIP").endcurrentedits

I may or may not have a typo somewhere for I can't quite recall what the
exact clause was.

BTW, it's better to use the datatable.getchanges to get a table of changes
to do the updates then to use the table itself.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top