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. (Last updated on: November 16, 2018).
Unfortunately, activation email could not send to your email. Please try again.
Syncfusion Feedback

Export Error

Thread ID:





132069 Aug 11,2017 12:35 AM UTC Aug 11,2017 12:36 PM UTC ASP.NET MVC 1
Tags: Grid
Fillipe Barbosa
Asked On August 11, 2017 12:35 AM UTC

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;


Prasanna Kumar Viswanathan [Syncfusion]
Replied On 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 


This post will be permanently deleted. Are you sure you want to continue?

Sorry, An error occured while processing your request. Please try again later.

Please sign in to access our forum

This page will automatically be redirected to the sign-in page in 10 seconds.

Warning Icon You are using an outdated version of Internet Explorer that may not display all features of this and other websites. Upgrade to Internet Explorer 8 or newer for a better experience.Close Icon