仅将数据添加到特定列 [英] Adding data only to a specific Column

查看:103
本文介绍了仅将数据添加到特定列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图找到一种将数据从一个数据网格添加到另一个数据的方法,并且该数据在第二个数据网格中一次只能插入一个列。每次点击添加按钮时,将创建特定列。

I'm trying to find a way to add data from one datagrid to another and for that data to be inserted to only one column at a time in my second datagrid. The specific column is created each time the Add button has been clicked.

我的编码到目前为止:

private void btnFeedbackAddSupplier_Click(object sender, RoutedEventArgs e)
{
    dgFeedbackSelectSupplier.Items.Clear(); //So that my rows do not stack each other on every add

    DataGridTextColumn columnSupplier = new DataGridTextColumn();
    columnSupplier.Binding = new Binding("Supplier");
    DataGridTextColumn columnFeedbackSupplierItem = new DataGridTextColumn();
    //The 'Item' column is binded in XAML
    columnSupplier.Header = (cmbFeedbackSelectSupplier.SelectedItem as DisplayItems).Name;
    columnSupplier.IsReadOnly = true;

    dgFeedbackAddCost.SelectAll(); //Selects all the rows in 1st datagrid

    //Casts selected rows to my 'ViewQuoteItemList' class
    IList list = dgFeedbackAddCost.SelectedItems as IList;
    IEnumerable<ViewQuoteItemList> items = list.Cast<ViewQuoteItemList>();

    var collection = (from i in items let a = new ViewQuoteItemList { Item = i.Item, Supplier = i.Cost }
                          select a).ToList();

    //Adds both the column and data to the 2nd datagrid
    dgFeedbackSelectSupplier.Columns.Add(columnSupplier);
    foreach (var item in collection)
        dgFeedbackSelectSupplier.Items.Add(item);
}

我希望将数据一次添加到一个单独的列的原因是因为每次我想将它添加到我的第二个datagrid数据是不同的,它覆盖以前添加的数据。

My reason for wanting to add the data to only one separate column at a time is because the data is different each time I want to add it to my 2nd datagrid and it overwrites any previous data that was entered in older add's.

编辑:我以下是我当前的问题的一些图片

EDIT: I Here are some images of what my current problem is

在这里我添加了第一家公司,它的价值观。一切工作正常

Here I add the first company and it's values. Everything works fine

这里我添加第二个公司,它的新值,但它会更改第一个公司输入的值。这是我的大问题。所以你可以看到我的价值观从第一个图像变为第二个图像。

Here I add the second company with it's new values, but it changes the values entered with the first company. This is my big problem. So you can see how my values are changed from the first to the second image

推荐答案

我认为您的问题在于所有的列都绑定到相同的属性:供应商。由于您每次更新该属性,因此所有列都将分配相同的值。最后,每行只有一个供应商属性,因此每次更改该属性的值时,不能为每个列显示该单个属性的不同值,绑定将被通知并进行更新。

I think your problem here is that all your columns are bound to the same property: Supplier. Since you're updating that property everytime, all columns are assigned the same value. In the end, there's only one Supplier property for each row, so you can't show a different value for that single property on each column since everytime you change that property's value, the Bindings get notified and update themselves.

也许您可以尝试使用 OneTime 绑定而不是常规绑定。这样,当您首次将它们添加到DataGrid时,单元格将保留它们具有的值。但是为了工作,您应该避免清除DataGrid的项目列表,因为重新添加项目将强制他们再次重新绑定。

Maybe you could try using a OneTime Binding instead of a regular one. That way, the cells would retain the value they had when you first added them to the DataGrid. But for that to work, you should avoid clearing the DataGrid's items list, since re-adding the items would force them to rebind again.

另一个选项将是一个列表的供应商属性,并将每列绑定到该列表的索引。

Another option would be having a list of suppliers in your Supplier property, and have each column bind to an index of that list.

private void btnFeedbackAddSupplier_Click(object sender, RoutedEventArgs e) 
{

    // ...        

    columnSupplier.Binding = new Binding(string.Format("Supplier[{0}]", supplierColumnIndex));

    // ...

    var supplierCosts = new List<int>();

    // ...
    // Fill the list with the Costs of the Suppliers that correspond to each column and item
    // ...

    var collection = (from i in items let a = new ViewQuoteItemList { Item = i.Item, Supplier = supplierCosts }
                          select a).ToList();

    //Adds both the column and data to the 2nd datagrid
    dgFeedbackSelectSupplier.Columns.Add(columnSupplier);
    foreach (var item in collection)
        dgFeedbackSelectSupplier.Items.Add(item); 
}

这篇关于仅将数据添加到特定列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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