I'm writing CRUD code for the WPF Datagrid.
In the TheDataGrid_CellEditEnding method below:
- how do I get the original text before the user made the change?
- I need the original text to be able to change the customer and save it back to the database with _db.SubmitChanges()
Here's the full solution with database if anyone wants to experiment with this:
http://www.tanguay.info/web/download/testDataGrid566northwindDatagrid.zip
XAML:
<toolkit:DataGrid x:Name="TheDataGrid"
AutoGenerateColumns="True"
CellEditEnding="TheDataGrid_CellEditEnding"/>
code-behind:
private void TheDataGrid_CellEditEnding(object sender, Microsoft.Windows.Controls.DataGridCellEditEndingEventArgs e)
{
//get the original text
Customer customer = e.Row.Item as Customer;
string customerID = customer.CustomerID;
int displayIndex = (int)e.Column.DisplayIndex; // e.g. equals 4 when user edits the 5th column
//HOW TO I GET THE ORIGINAL TEXT? THERE IS NO FIELDS METHOD IN THE LINQ-TO-SQL CLASSES
string originalText = customer.Fields[displayIndex].value.ToString();
//get the changed text
TextBox changedTextBox = e.EditingElement as TextBox;
string changedText = changedTextBox.Text;
//inform user
Message.Text = String.Format("cell was changed from {0} to {1}", originalText, changedText);
//I NEED TO CHANGE THE CUSTOMER WITH THE ABOVE TEXT
//BEFORE I SAVE IT BACK HERE
_db.SubmitChanges();
}