如何获取此CRUD代码在WPF DataGrid上工作? [英] How to do get this CRUD code to work on WPF DataGrid?

查看:125
本文介绍了如何获取此CRUD代码在WPF DataGrid上工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为WPF Datagrid编写CRUD代码。



在下面的TheDataGrid_CellEditEnding方法中:




  • 如何在用户进行更改前如何获取原始文本?

  • 我需要原始文本才能更改客户并将其保存回数据库与_db.SubmitChanges()



下面是数据库的完整解决方案,如果有人想试验这个:



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



XAML:

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

代码隐藏

  private void TheDataGrid_CellEditEnding(object sender,Microsoft.Windows.Controls.DataGridCellEditEndingEventArgs e)
{
//获取原始文本
客户端customer = e.Row.Item作为客户;
string customerID = customer.CustomerID;
int displayIndex =(int)e.Column.DisplayIndex; //例如当用户编辑第5列时,等于4

//如何获取原始文本?在LINQ-to-SQL类中没有字段方法
string originalText = customer.Fields [displayIndex] .value.ToString();

//获取更改的文本
TextBox changedTextBox = e.EditingElement作为TextBox;
string changedText = changedTextBox.Text;

//通知用户
Message.Text = String.Format(单元格从{0}更改为{1},originalText,changedText);

//我需要用上面的文本更改客户
//在我保存它之前,
_db.SubmitChanges();
}


解决方案

文本?是否显示一些信息消息?



在您的情况下,您似乎将datagrid绑定到您的LinqToSQL对象。这意味着该行被绑定的Customer对象已经被更新,并且你需要做的就是调用SubmitChanges()。


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();
}

解决方案

Why do you need the original text? Is it to display some informational message?

In your case, you seem to be binding the datagrid to your LinqToSQL objects. This means that the Customer object the row is bound to is already updated and all you need to do is call SubmitChanges().

这篇关于如何获取此CRUD代码在WPF DataGrid上工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆