Adding data only to a specific Column

后端 未结 1 439
南旧
南旧 2021-01-28 16:29

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 cr

相关标签:
1条回答
  • 2021-01-28 16:59

    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.

    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); 
    }
    
    0 讨论(0)
提交回复
热议问题