将DataGrid.ItemSource转换为DataTable

时间:2016-02-04 12:07:41

标签: c# wpf datagrid datatable

我需要将当前DataGrid的Item Soucrce及其所有内容转换为新的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];
            }
        }
    }

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

  

无法投射类型的对象   'System.Collections.Generic.List`1 [MKCWorkflowApplication.ViewQuoteItemList]'   输入'System.Data.DataView'。

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

修改

我的班级'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); }
    }
}

1 个答案:

答案 0 :(得分:2)

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

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

如果这没有帮助,您应该向我们展示课程ViewQuoteItemList

相关问题