Answer the question
In order to leave comments, you need to log in
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
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 questionAsk a Question
731 491 924 answers to any question