My code has a Grid, which has dynamic columns (created in the controller). Follows the controller code and view.
Controller:
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):
@(Html.EJ().Grid<object>("Grid")
.Locale("pt-BR")
.Datasource((IEnumerable<object>)ViewBag.DataSource)
.ToolbarSettings(toolBar => toolBar.ShowToolbar().ToolbarItems(items =>
{
items.AddTool(ToolBarItems.ExcelExport);
}))
.AllowFiltering()
.FilterSettings(filter => { filter.FilterType(FilterType.Excel); })
.AllowSorting()
.AllowGrouping()
.SelectionType(SelectionType.Multiple)
.AllowResizing()
.Columns(ViewBag.Columns))
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?
Thanks.