【发布时间】:2011-08-21 14:58:40
【问题描述】:
大家好,我收藏了:
public class ActionData
{
private int iD;
public int ID
{
get { return iD; }
set { iD = value; }
}
private string roomType;
public string RoomType
{
get { return roomType; }
set { roomType = value; }
}
}
喜欢这个
private void btnGridToExcel_Click(object sender, RoutedEventArgs e)
{
ExportExcel<ActionData, ActionDatas> exp = new ExportExcel<ActionData, ActionDatas>();
exp.GenerateReport();
ICollectionView view = CollectionViewSource.GetDefaultView(dataGrid1.ItemsSource);
exp.dataToPrint = (ActionDatas)view.SourceCollection;
exp.GenerateReport();
}
在此按钮上单击必须将数据导出到 excel 但我在exp.dataToPrint = (ActionDatas)view.SourceCollection; 上给出了这样的错误:
无法将“System.Collections.ObjectModel.ObservableCollection`1[H_Pro.Logicstic+ActionData]”类型的对象转换为“ActionDatas”类型。
这是获取值的方法的一部分
public class ActionDatas : List<ActionData> { }
#region toxls
public class ExportExcel<T, U>
where T : class
where U : List<T>
{
public List<T> dataToPrint;
有人知道为什么我会收到这样的错误吗?
【问题讨论】:
-
填充 view.SourceCollection 的代码在哪里?
-
ICollectionView 视图 = CollectionViewSource.GetDefaultView(dataGrid1.ItemsSource);
-
如何设置
dataGrid1.ItemsSource?从源创建视图然后仅使用该源的目的是什么? -
@svick 这样做有很多原因 - 您可能希望在 UI 中对数据进行排序,但需要在用户单击此按钮后对其原始形式的列表进行原始访问。
-
@cunningdave,但是,据我所知,视图永远不会靠近 UI。它只是用来访问它的源,没有别的。