I was able to set up a WPF Datagrid, display a Northwind database table via linq-to-sql, and handle the TheDataGrid_RowEditEnding event so that it saves back the the database.
However, when a CustomerID was changed, it gets an error from the database which I handle, but how do I now either (1) rollback the Datagrid control or (2) refetch the original data from the database view LINQ-to-SQL (the refetching that I do below via LINQ seems to have some kind of caching, it doesn't refresh):
<Grid DockPanel.Dock="Bottom">
<toolkit:DataGrid x:Name="TheDataGrid"
AutoGenerateColumns="True"
RowEditEnding="TheDataGrid_RowEditEnding"/>
</Grid>
private void TheDataGrid_RowEditEnding(object sender, Microsoft.Windows.Controls.DataGridRowEditEndingEventArgs e)
{
try
{
_db.SubmitChanges();
}
catch (Exception ex)
{
RefreshData();
Message.Text = ex.Message;
}
}
public void RefreshData()
{
var customers = from c in _db.Customers
select c;
TheDataGrid.ItemsSource = customers;
}
ANSWER:
Thanks Denis, I used your suggestions to get what I was after:
private void TheDataGrid_RowEditEnding(object sender, Microsoft.Windows.Controls.DataGridRowEditEndingEventArgs e)
{
try
{
_db.SubmitChanges();
}
catch (Exception ex)
{
Customer customer = e.Row.Item as Customer;
_db.Refresh(System.Data.Linq.RefreshMode.OverwriteCurrentValues, customer);
Message.Text = ex.Message;
}
}