(the underlines are for my benefit only. It's hard to keep the data-thingy's straight)
I cannot get the database to update after a cell changed in a wpf datagrid. Can someone tell me what I'm doing wrong?
Code to set dataadapter and datatable:
cmd = New SqlCommand(sSelect, conn) sda = New SqlDataAdapter(cmd) dt = New DataTable("xxxx") sda.Fill(dt)
Code to determine if cell changed and write back to database:
Private Sub dgDentalAppointments_CellEditEnding(sender As Object, e As System.Windows.Controls.DataGridCellEditEndingEventArgs) Handles dgDentalAppointments.CellEditEnding 'These 2 rows of code here and CurrentCellChanged borrowed from http://www.scottlogic.com/blog/2009/01/21/wpf-datagrid-committing-changes-cell-by-cell.html Dim rowView As DataRowView = TryCast(e.Row.Item, DataRowView) rowBeingEdited = rowView CommitEdit() End Sub Private Sub dgDentalAppointments_CurrentCellChanged(sender As Object, e As System.EventArgs) Handles dgDentalAppointments.CurrentCellChanged If rowBeingEdited IsNot Nothing AndAlso dgDentalAppointments.CurrentCell.Item IsNot rowBeingEdited Then rowBeingEdited.EndEdit() End If End Sub Private Sub CommitEdit() 'code borrowed from http://codefluff.blogspot.com/2010/05/commiting-bound-cell-changes.html If Not isManualEditCommit Then isManualEditCommit = True dgDentalAppointments.CommitEdit(DataGridEditingUnit.Row, True) Dim Row As DataRow = dt.Rows(0) dt.AcceptChanges() Dim builder As SqlCommandBuilder = New SqlCommandBuilder(sda) sda.UpdateCommand = builder.GetUpdateCommand sda.Update(dt) isManualEditCommit = False End If End Sub
At this point, I'm trying to update existing rows in the database only. I'll try adding and deleting rows when I get this figured out.
The datagrid is binding to the datatable successfully.
The datatable is being updated successfully.
I just can't get the database to update successfully. I expect it to update at sda.Update(dt)
Thanks for any help or advice. Let me know if I need to post more code.
Kris Hood