I've got a form with a datagrid that displays a list of items, along
with several textboxes, comboboxes, etc., that display the details for
the selected item. The columns in the datagrid and the "details"
controls are both bound to fields in a dataview called dvItems. The
dataview is a subset of a datatable called dtItems, which is simply a
copy of all the data in a table called Items. The idea is to allow
the user to edit an item in either the datagrid or in the 'details'
form below it.
My initial problem was getting GetChanges to actually reflect the
changes that the user made. I tried calling
BindingContext(dtItems).EndCurrentEdit(), but that didn't seem to do
anything (also tried it with dvItems). The only way I could get it to
work was to add a dummy row to the table, then remove it, and then
change the CurrentRowIndex to 0 and then back to the previous index,
like so:
Private Sub SaveItemGridChanges()
BindingContext(dtItems).EndCurrentEdit()
Dim row As DataRow
row = dtItems.NewRow()
dtItems.Rows.Add(row)
dtItems.Rows.Remove(row)
Dim intRowIndex As Integer
intRowIndex = dgItems.CurrentRowIndex
dgItems.CurrentRowIndex = 0
dgItems.CurrentRowIndex = intRowIndex
If Not dtItems.GetChanges Is Nothing Then
MessageBox.Show("Changed")
daMain = New OleDbDataAdapter(strItemSelectSQL,
dbConn)
Dim command_builder As OleDb.OleDbCommandBuilder
command_builder = New
OleDb.OleDbCommandBuilder(daMain)
daMain.Update(dtItems)
End If
End Sub
Seems stupid, but it works. When the user clicks the save button, the
data is correctly updated with changes to either the datagrid or the
controls on the item details form below it.
Now I've got another problem, though. I've got a "Deactivate Item"
button which is supposed to set the status column for the currently
selected to "Inactive" and then save the record. I've got a combobox
that allows the user to manually change the status of an item, but
this button is supposed to provide a shortcut. I can't figure out a
way to directly update the column in the "current record" of the
dataview, so I figured I'd take the easy way out: Simply
programmatically change the value of the status combobox to the
correct value and then call the SaveItemGridChanges Sub.
The problem is that although the value of the combobox changes to
Inactive (cboStatus.SelectedValue = 6), it just changes back when I
call the SaveItemGridChanges sub. I'm not sure if this has to do with
the contortions this Sub goes thru to get the changes to be recognized
or what. I've tried taking out some or all of the code that forces
the GetChanges function to work, but then it just doesn't save. When
I manually change the status and the click the save button (which just
calls SaveItemGridChanges), it works fine. So apparently some event
is firing when I manually make the change that doesn't fire when I
programmatically set the value.
I've been working on this same stupid problem for about 5 hours now,
so any help would be greatly appreciated.