gpt4 book ai didi

c# - 将 DataGrid.ItemSsource 转换为 DataTable

转载 作者:行者123 更新时间:2023-11-30 23:29:51 25 4
gpt4 key购买 nike

我需要将当前 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];
}
}
}

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

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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com