How to make CRUD code to work with WPF DataGrid?

I am writing CRUD code for WPat Datagrid.

In TheDataGrid_CellEditEnding method below:

  • How to get the source text before the user made the changes?
  • I need the source code to be able to modify the client and save it back to the database using _db.SubmitChanges ()

Here's a complete solution with a database if someone wants to experiment with this:

http://www.tanguay.info/web/download/testDataGrid566northwindDatagrid.zip

XAML:

<toolkit:DataGrid x:Name="TheDataGrid" 
                  AutoGenerateColumns="True"
                  CellEditEnding="TheDataGrid_CellEditEnding"/>

background code:

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();
}
+2
source share
2 answers

Why do you need the original text? Should some kind of informational message be displayed?

, , datagrid LinqToSQL. , Customer, , , , , SubmitChanges().

+1

Source: https://habr.com/ru/post/1705243/


All Articles