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

NullReferenceException on exporting Grid with dynamically columns

My code has a Grid, which has dynamic columns (created in the controller). Follows the controller code and view.


public ViewResult GridFeatures(){

            var DataSource = new NorthwindDataContext().OrdersViews.ToList();
            ViewBag.datasource = DataSource;
            List<Column> columns = new List<Column>();
            columns.Add(new Column() { Field = "OrderID", HeaderText = "OrderID", Width = 70 });
            columns.Add(new Column() { Field = "CustomerID", HeaderText = "CustomerID", Width = 70 });
            columns.Add(new Column() { Field = "EmployeeID", HeaderText = "EmployeeID", Width = 70 });
            columns.Add(new Column() { Field = "Freight", HeaderText = "Freight", Width = 70 });
            ViewBag.Columns = columns;

            return View();

public void ExportToExcel(string GridModel)
            ExcelExport exp = new ExcelExport();
            var DataSource = new NorthwindDataContext().OrdersViews.ToList();
            GridProperties obj = ConvertGridObject(GridModel);
            exp.Export(obj, DataSource, "Export.xlsx", ExcelVersion.Excel2010, 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);
                    object value = serializer.Deserialize(serialize, type);
                    property.SetValue(gridProp, value, null);
            return gridProp;

View (Only the part where I set the Grid):

            .ToolbarSettings(toolBar => toolBar.ShowToolbar().ToolbarItems(items =>
            .FilterSettings(filter => { filter.FilterType(FilterType.Excel); })

When trying to export reports in Excel, Word or PDF an error occurred:

Object reference not set to an instance of an object.
Linha 28:                     ExcelExport exp = new ExcelExport();
Linha 29:                     var DataSource = new NorthwindDataContext().OrdersViews.ToList();
Linha 30:                     GridProperties obj = ConvertGridObject(GridModel);
Linha 31:                     exp.Export(obj, DataSource, "Export.xlsx", ExcelVersion.Excel2010, false, false, "flat-saffron");
Linha 32:             }

Stack trace:

[NullReferenceException: Object reference not set to an instance of an object.]
Syncfusion.EJ.Export.GridExcelExport.ProcessRecordCell(Object row, Column column) +1037 Syncfusion.EJ.Export.<>c__DisplayClasse.<ProcessRecordRow>b__d(Column column) +464 System.Collections.Generic.List`1.ForEach(Action`1 action) +11439282 Syncfusion.EJ.Export.GridExcelExport.ProcessRecordRow(Object row) +282

What might be happening?


1 Reply

BM Balaji Marimuthu Syncfusion Team April 27, 2016 05:19 AM UTC

Hi André, 
Thanks for contacting Syncfusion support. 
We have created a sample and checked the issue by using the provided code snippets, but we were unable to reproduce the reported issue. Refer to the sample in following link. 
The mentioned issue is occurred, due to when the columns bound to the Grid are not defined in the Grid dataSource. We have already discussed the same issue in following knowledge base doucument. 
For example if the column field name “Employee1” is not defined in Grid dataSource, the null exception will be thrown while exporting the Grid dataSource. So, please ensure the columns bound to the Grid are defined in the Grid dataSource before exporting the Grid. Still if you face the issue, kindly share the following information. 
1.     Scenario to reproduce the issue. 
2.     Essential studio and browser version details. 
3.     Share sample if possible or modify the attached sample as issue reproducible. 
Balaji Marimuthu 

Live Chat Icon For mobile
Up arrow icon