c# - 将数据添加到 DataGrid 中的特定列

标签 c# wpf datagrid

我想创建一个新列,然后在按钮单击事件中向该新列添加值。那可能吗?该列下方可能有几行,具体取决于报价中的项目数量。

到目前为止我取得的成就:

我的所有信息都存储在我的类(class)

public class ViewQuoteItemList
{
    ...
    public double SupplierCost { get; set; }
    ...
}

我可以创建我的专栏并将其绑定(bind)到我的 ViewQuoteItemList

DataGridTextColumn columnFeedbackSupplier = new DataGridTextColumn();
columnFeedbackSupplier.Binding = new Binding("SupplierCost");
//The header of the column gets it's value from a combobox where you select a company to be added to the datagrid
columnFeedbackSupplier.Header = (cmbFeedbackSelectSupplier.SelectedItem as DisplayItems).Name;

从这里我从不同的数据网格中获取我的报价项目,并将它们添加到我想要添加新列及其值的第二个数据网格中

IList list = dgFeedbackAddCost.SelectedItems as IList;
IEnumerable<ViewQuoteItemList> items = list.Cast<ViewQuoteItemList>();

var collection = (from i in items
                  let a = new ViewQuoteItemList { SupplierCost = 0 }
                  select a).ToList();

最后,我将新列添加到第二个数据网格并将 collection 设置为数据网格的 ItemSource

dgFeedbackSelectSupplier.Columns.Add(columnFeedbackSupplier);
dgFeedbackSelectSupplier.ItemsSource = collection;

我的问题是,一旦我编辑了来自供应商之一的数据单元格,整行就会更新为那个值,因为它都绑定(bind)到一个类/项目源。这可以修复吗?

编辑:

“整行更新”意味着每次我将一个值插入一行中的一个单元格时,该行中的每个单元格都会更新为相同的值。这里有一些图片显示了我的意思。我想编辑所有数据,这一切都发生在我的第二个数据网格 (dgFeedbackSupplier) 上。

在这里,我添加了两家公司以及我想要比较价格的 4 个项目。现在我想单击公司下方的单个单元格并为特定项目添加值。

enter image description here

在这里,我双击一个单元格来编辑/更改值。 enter image description here

然后,当我更改所选单元格中的值时,同一行中该特定项目的所有其他公司的值都会更新为相同的值。 enter image description here

这是我的问题,我只需要更改一个单元格的值,而不是整行。

编辑 2:

如何将此集合转换为ExpandoObject

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

编辑 3:

我的 XAML:

<DataGrid x:Name="dgFeedbackSelectSupplier"  Margin="245,266,0,32" BorderBrush="#FFADADAD" ColumnWidth="*" AutoGenerateColumns="True" CanUserAddRows="False">
    <DataGrid.Columns>
        <DataGridTextColumn x:Name="columnFeedbackSupplierItem" IsReadOnly="True" Header="Item" Binding="{Binding Item}"/>
    </DataGrid.Columns>
</DataGrid>

这就是我添加列和从其他数据网格获取项目时我的整个方法的样子:

    private void btnFeedbackSelectSupplier_Click(object sender, RoutedEventArgs e)
    {
        supplier.Id = (cmbFeedbackSelectSupplier.SelectedItem as DisplayItems).Id;//Not using yet
        supplier.Name = (cmbFeedbackSelectSupplier.SelectedItem as DisplayItems).Name;

        DataGridTextColumn columnFeedbackSupplier = new DataGridTextColumn();
        columnFeedbackSupplier.Binding = new Binding("Supplier") { Mode = BindingMode.TwoWay };
        columnFeedbackSupplier.CanUserReorder = true;
        columnFeedbackSupplier.CanUserResize = true;
        columnFeedbackSupplier.IsReadOnly = false;

        columnFeedbackSupplier.Header = supplier.Name;

        dgFeedbackAddCost.SelectAll();

        IList list = dgFeedbackAddCost.SelectedItems as IList;
        IEnumerable<ViewQuoteItemList> items = list.Cast<ViewQuoteItemList>();

        var collection = new List<ExpandoObject>();
        foreach (var i in items)
        {
            dynamic a = new ExpandoObject();
            a.Id = (cmbFeedbackSelectSupplier.SelectedItem as DisplayItems).Id;
            a.Item = i.Item;
            a.Supplier = 25;
            collection.Add(a);
        }

        dgFeedbackSelectSupplier.Columns.Add(columnFeedbackSupplier);
        dgFeedbackSelectSupplier.ItemsSource = collection;
    }

最佳答案

我可以提出另一种方法吗?据我了解;

您有供应商,这些供应商有元素。您可以添加新的供应商。我假设供应商可能没有所有元素(挑战 :))。

您想在类似网格的 View 中显示此数据结构。

这里的问题是您正在尝试使用表格 View 组件显示非表格数据。您拥有的是分层数据。在继续我的解决方案之前,这里有一些屏幕截图。

enter image description here

enter image description here

enter image description here

我在这里所做的基本上是在分层数据上创建新 View ,填补空白并将其转换为表格形式。我为空槽使用了假类,这样我就可以轻松地在 XAML 中选择正确的数据模板。我避免使用任何自定义代码并以 MVVM+XAML 方式保留所有内容。因此绑定(bind)和此类工作按预期进行。

当集合发生变化时,必须更新新 View ,因此我使用 MVVMLight 中的 messenger 类来轻松实现,并手动调用 RaisePropertyChanged 事件。

在 XAML 中,我使用 ItemsControl 和 UniformGrid 组件来创建类似网格的 View 。

我把完整的解决方案放在 GitHub 上: https://github.com/orhtun/GridLikeViewWithDynamicColumns

它会在每次运行时创建随机数据。如果您遇到构建错误,请尝试右键单击解决方案并恢复 NuGet 包。

关于c# - 将数据添加到 DataGrid 中的特定列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35221658/

相关文章:

c# - 为什么不将所有服务类都集中到一个工厂方法中(而不是注入(inject)接口(interface))?

c# - 鼠标悬停时出现弹出窗口

wpf - WPF DataGrid列交替着色

c# - DataGridRowTemplateColumn - 高效地重构和使用样式?

c# - 在同一 View 中创建多个相同类型的对象

c# - 持久资源管理器 (IEnlistmentNotification) 恢复

c# - 控制台按键不起作用?

c# - Windows Classic 主题上的 WPF Tabcontrol TabItem 显示问题

WPF ControlTemplate 部分替换

silverlight - 如何在 Silverlight 中使用 MVVM 根据绑定(bind)到的数据更改 DataGrid 的单元格?