Hello guys i have collection :
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; }
}
}
like this
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();
}
On this button click must export data to excel but i gives such error on exp.dataToPrint = (ActionDatas)view.SourceCollection;:
Unable to cast object of type ‘System.Collections.ObjectModel.ObservableCollection`1[H_Pro.Logicstic+ActionData]’ to type ‘ActionDatas’.
Here is a some part of method which gets values
public class ActionDatas : List<ActionData> { }
#region toxls
public class ExportExcel<T, U>
where T : class
where U : List<T>
{
public List<T> dataToPrint;
does anyone have an idea why im getting such error?
The data from ActionDatas might be implicitly being assigned to SourceCollection (possibly in XAML) which is an ObservableCollection<> (not the same thing as a List<>). It being an ObservableCollection is a good thing, since collection changes will automatically notify the listening WPF UI.
You should be able to simply assign an ActionDatas, and transfer the elements:
Or something along those lines.
Update: If your datagrid ItemsSource is already an ActionDatas object, then you should assign it directly to the dataToPrint member, no need to put it in a view and then transfer back into a new collection.