将 DataGrid.ItemSource 转换为 DataTable
本文关键字:DataTable 转换 ItemSource DataGrid | 更新日期: 2023-09-27 18:33:09
我需要将当前 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 转换为新的数据表,如下所示:
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]' 键入"系统.数据.数据视图"。
我不知道它为什么要这样做,因为我认为任何数据网格的项源都可以简单地转换为新的数据表。有没有办法解决这个问题?
编辑:
我的班级"查看报价项目列表"
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
?您不需要数据表,可以使用以下 LINQ 查询。
var source = (List<ViewQuoteItemList>) dgFeedbackSelectSupplier.ItemsSource;
int sum = source.Sum(x => x.Prop1 + x.Prop2 + x.Prop3); // change accordingly
如果这没有帮助,您应该向我们展示班级ViewQuoteItemList
。