I
I
Ilya2014-05-20 06:02:41
WPF
Ilya, 2014-05-20 06:02:41

How to save changes in the database when entering data in a DataGrid row?

I draw in WPF. I use MS SQL Server 2008. Created xsd dataset.
The output of data to the table was carried out through ObservableCollection. But how to save the changes after entering the data immediately into the database (without buttons, just after editing the line)?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
snuffi, 2014-09-27
@snuffi

Subscribe to the collection change event
private void DataGrid_Loaded(object sender, RoutedEventArgs e)
{
var dg = (DataGrid)sender;
if (dg == null || dg.ItemsSource == null) return;
var sourceCollection = dg.ItemsSource as ObservableCollection;
if (sourceCollection == null) return;
sourceCollection .CollectionChanged +=
new NotifyCollectionChangedEventHandler(DataGrid_CollectionChanged);
}
void DataGrid_CollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
{
//write to database here
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question