I am using WPF datagrid
bound to a database via the Visual Studio IDE drag and drop, and found a way to have my datagrid
update and add to the database in real time using the RowEditEnding
event thusly:
private void Update_WatchList(object sender,
DataGridRowEditEndingEventArgs e)
{
UpdateWatchList();
}
private void UpdateWatchList()
{
dsDocControlTableAdapters.DocumentWatchListTableAdapter ta =
new dsDocControlTableAdapters.DocumentWatchListTableAdapter();
try
{
DataRowView dr =
(DataRowView)documentWatchListDataGrid.SelectedItem;
dsDocControl.DocumentWatchListRow dr1 =
(dsDocControl.DocumentWatchListRow)dr.Row;
dr1.EndEdit();
if (dr1.RowState == DataRowState.Detached)
dsDocControl.DocumentWatchList.AddDocumentWatchListRow(dr1);
ta.Update(dr1);
}
catch (Exception ex)
{
MessageBox.Show("Error UpdateWatchList:\n" + ex.Message);
}
}
This was the result of much trial and error (during which time I missed, rescheduled and missed again a dental appointment) but somehow stumbled across the need to call EndEdit()
on the row for the RowState
to be updated to either Modified or Detached. Is this the expected sequence of operations for what seems to be a very normal use case, or is there a better way to do this?
I had started to go the route of adding a CollectionChanged
event handler for the added rows, but could not get it to work probably because I don't properly use the ObservableCollection<T>
:
private void documentWatchListDataGrid_Loaded(object sender,
RoutedEventArgs e)
{
var dg = (DataGrid)sender;
if(dg==null || dg.ItemsSource == null) return;
var sourceCollection = dg.ItemsSource as ObservableCollection<DataRow>;
if (sourceCollection == null) return;
//sourceCollection.CollectionChanged +=
new NotifyCollectionChangedEventHandler(Update_WatchList); // Never gets called.
}
The line that adds the handler is commented out because I used the handler as the RowEditEnding
(in the first code block) but regardless it never to to that point because sourceCollection
was always null.
My question is, what is the best practices way to do this seemingly simple thing: update in real time as each row is changed or added in a WPF datagrid
control?