We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. Image for the cookie policy date

Export Error

Good Night,

I'm trying to export the grid for excel or pdf with the filters, like the example https://www.syncfusion.com/forums/119826/export-filtered-grid-to-excel, but i'm getting the error: System.InvalidCastException: 'Unable to cast object of type 'System.Object' to type 'System.Collections.IEnumerable'.' at strong and sub line:

My code:

public void ExportToPdf(string GridModel)
            PdfExport exp = new PdfExport();
            var DataSource = new DB_DBEntities().vPerfil.ToList();
            GridProperties obj = (GridProperties)ConvertGridObject(GridModel);
            exp.Export(obj, (IEnumerable)obj.DataSource, "Export.pdf", false, false, "flat-saffron");
        private GridProperties ConvertGridObject(string gridProperty)
            JavaScriptSerializer serializer = new JavaScriptSerializer();
            IEnumerable div = (IEnumerable)serializer.Deserialize(gridProperty, typeof(IEnumerable));
            GridProperties gridProp = new GridProperties();
            foreach (KeyValuePair<string, object> ds in div)
                var property = gridProp.GetType().GetProperty(ds.Key, BindingFlags.Instance | BindingFlags.Public | BindingFlags.IgnoreCase);
                if (property != null)
                    Type type = property.PropertyType;
                    string serialize = serializer.Serialize(ds.Value);
                    if (ds.Key == "dataSource")
                        List<vPerfil> value1 = serializer.Deserialize<List<vPerfil>>(serialize);
                        property.SetValue(gridProp, value1, null);

                    object value = serializer.Deserialize(serialize, type);
                    property.SetValue(gridProp, value, null);
            return gridProp;


1 Reply

PK Prasanna Kumar Viswanathan Syncfusion Team August 11, 2017 12:36 PM UTC

Hi Fillipe, 

Thanks for contacting Syncfusion support. 

According to your requirement you need to export the Grid to Excel or PDF with the filtered data. By default, the grid has inbuilt filtering support. In server-side we pass the grid model as a parameter in export method. After we filter the grid the filtered column details will be stored on the filterColumns property of the gridmodel. So, while exporting it will export only the filtered data. If you perform filtering operation using Syncfusion Grid filters(filterBar, menu and Excel) then it will export only the filtered data while exporting.  
Find the code example and sample:  

    .ToolbarSettings(toolBar => toolBar.ShowToolbar().ToolbarItems(items => 
    .Columns(col => 
public void ExportToExcel(string GridModel) 
            ExcelExport exp = new ExcelExport(); 
            var DataSource = new NorthwindDataContext().OrdersViews.ToList(); 
            GridProperties obj = ConvertGridObject(GridModel); 
            exp.Export(obj, data, "Export.xlsx", ExcelVersion.Excel2013, false, false, "flat-saffron"); 

In Forum 119826 the customer using the external filter for grid. In that forum the customer has bind the data in grid with his store procedure using the params and  while exporting he need to export the data with his store procedure. So, while using external filter we need to pass filter data in export function.  

Prasanna Kumar N.S.V 

Live Chat Icon For mobile
Up arrow icon