c# - 将 DataGrid.ItemSsource 转换为 DataTable

标签 c# wpf datagrid datatable

我需要将当前 DataGrid 的项目源及其所有内容转换为新的 DataTable。

这是我将所有信息插入 dgFeedbackSelectSupplier 的 ItemSource 的编码

private void btnFeedbackSelectSupplier_Click(object sender, RoutedEventArgs e)
{
    DataGridTemplateColumn columnFeedbackSupplier = new DataGridTemplateColumn();
    columnFeedbackSupplier.Header = "Supplier";
    columnFeedbackSupplier.CanUserReorder = true;
    columnFeedbackSupplier.CanUserResize = true;
    columnFeedbackSupplier.IsReadOnly = false;

    var stackPanel = new FrameworkElementFactory(typeof(StackPanel));
    stackPanel.SetValue(StackPanel.OrientationProperty, Orientation.Horizontal);

    DataTemplate cellTemplate = new DataTemplate();

    FrameworkElementFactory factoryCheck = new FrameworkElementFactory(typeof(CheckBox));
    Binding bindCheck = new Binding("TrueFalse");
    bindCheck.Mode = BindingMode.TwoWay;
    factoryCheck.SetValue(CheckBox.IsCheckedProperty, bindCheck);
    stackPanel.AppendChild(factoryCheck);

    FrameworkElementFactory factoryText = new FrameworkElementFactory(typeof(TextBox));
    Binding bindText = new Binding("Supplier");
    bindText.Mode = BindingMode.TwoWay;
    factoryText.SetValue(TextBox.TextProperty, bindText);
    stackPanel.AppendChild(factoryText);

    cellTemplate.VisualTree = stackPanel;
    columnFeedbackSupplier.CellTemplate = cellTemplate;

    DataGridTextColumn columnFeedbackSupplierItem = new DataGridTextColumn();
    columnFeedbackSupplier.Header = (cmbFeedbackSelectSupplier.SelectedItem as DisplayItems).Name;

    dgFeedbackAddCost.SelectAll();

    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 = 25, TrueFalse = false } //Remove Supplier(it's for test only)
                      select a).ToList();

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

    lblFeedbackRemoveCompany.Content = collection.ToList().Sum(x => x.Supplier);
}

在下一个事件中,我尝试将该 ItemSource 转换为新的 DataTable,如下所示:

    private void btnFeedbackGetTotals_Click(object sender, RoutedEventArgs e)
    {
        DataTable dt = new DataTable();
        dt = ((DataView)dgFeedbackSelectSupplier.ItemsSource).ToTable(); //Error here
        var sum = 0;
        foreach (DataRow dr in dt.Rows)
        {
            foreach (DataColumn dc in dt.Columns)
            {
                sum += (int)dr[dc];
            }
        }
    }

但是当我运行上述方法时,我收到以下错误:

Unable to cast object of type 'System.Collections.Generic.List`1[MKCWorkflowApplication.ViewQuoteItemList]' to type 'System.Data.DataView'.

我不知道为什么要这样做,因为我认为任何数据网格的项目源都可以简单地转换为新的数据表。有办法解决这个问题吗?

编辑:

我的类(class)“ViewQuoteItemList”

public class ViewQuoteItemList
{
    public string CustomerRFQ { get; set; }
    public int QuoteId { get; set; }
    public int Id { get; set; }
    public string Item { get; set; }
    public string Material { get; set; }
    public string Description { get; set; }
    public string AdditionalInformation { get; set; }
    public int Quantity { get; set; }
    public string Cost { get; set; }
    public decimal Supplier { get; set; }
    public bool TrueFalse { get; set; }

    public string CheckBoxColumn
    {
        get { return string.Format("{0} {1}", Supplier, TrueFalse); }
    }
}

最佳答案

为什么要转换强类型 List<ViewQuoteItemList>松散类型 DataTable根本吗?您不需要DataTable,您可以使用以下LINQ 查询。

var source = (List<ViewQuoteItemList>) dgFeedbackSelectSupplier.ItemsSource;
int sum = source.Sum(x => x.Prop1 + x.Prop2 + x.Prop3); // change accordingly

如果这没有帮助,您应该向我们展示类(class) ViewQuoteItemList .

关于c# - 将 DataGrid.ItemSsource 转换为 DataTable,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35200624/

相关文章:

c# - 为什么 ItemContainerGenerator.ContainerFromIndex() 返回 null 以及如何避免这种行为?

c# - 无法将表达式类型转换为返回类型

c# - 工作簿和_workbook之间的区别

c# - 使用 LINQ 和高度嵌套的 XML 创建类对象

c# - 如何禁用通过 COM 公开的 WPF HwndSources 的 DPI 缩放?

actionscript-3 - 单独验证 DataGridColumn 单元格

c# - 如何知道文本是否大于文本框?

c# - LINQ 中的自连接与聚合查询

不同颜色的 WPF 进度条

c# - 单击按钮更新 wpf 数据网格