When I call the ExportToExcel extension method like:
var options = new ExcelExportingOptions();
options.ExcelVersion = ExcelVersion.Excel2013;
var excelEngine = datagrid1.ExportToExcel(datagrid1.View, options);
var workBook = excelEngine.Excel.Workbooks[0];
I get a null reference exception if I have more than one GridViewDefinition in a nested DetailsViewDefinition.If the DetailsViewDefinition is
at the first level then multiple GridViewDefinitions works.
My XAML looks like this:
<syncfusion:SfDataGrid x:Name="datagrid1" HideEmptyGridViewDefinition="True" ItemsSource="{Binding CommissionPlans}" SelectedItem="{Binding SelectedPlan}"
AutoGenerateColumns="False" Margin="5" AllowResizingColumns="True" AutoGenerateRelations="True" ColumnSizer="Auto">
<syncfusion:SfDataGrid.Columns>
<syncfusion:GridTextColumn HeaderText="Location" MappingName="StoreName" MinimumWidth="200" />
<syncfusion:GridDateTimeColumn HeaderText="Start" MappingName="PlanBegin" />
<syncfusion:GridDateTimeColumn HeaderText="End" MappingName="PlanEnd" />
<syncfusion:GridTextColumn HeaderText="Rep" MappingName="RepName" MinimumWidth="175"/>
<syncfusion:GridTextColumn HeaderText="Is Active" MappingName="IsActive" />
<syncfusion:GridTextColumn HeaderText="Plan Type" MappingName="PlanType" />
<syncfusion:GridCurrencyColumn HeaderText="Base Pay" MappingName="BasePay" />
<syncfusion:GridCurrencyColumn HeaderText="Min. Comm." MappingName="MinComm" />
<syncfusion:GridPercentColumn HeaderText="Split Comm." MappingName="SplitCommRate" PercentEditMode="PercentMode" />
</syncfusion:SfDataGrid.Columns>
<syncfusion:SfDataGrid.DetailsViewDefinition>
<syncfusion:GridViewDefinition RelationalColumn="Incentives">
<syncfusion:GridViewDefinition.DataGrid>
<syncfusion:SfDataGrid x:Name="FirstLevelNestedGrid1" AllowResizingColumns="True"
AutoGenerateColumns="False" AutoGenerateRelations="False" HideEmptyGridViewDefinition="True">
<syncfusion:SfDataGrid.Columns>
<syncfusion:GridTextColumn HeaderText="Incentive" MappingName="IncentiveTypeID" />
<syncfusion:GridTextColumn HeaderText="Based On" MappingName="PIATypeID"/>
<syncfusion:GridTextColumn HeaderText="Category" MappingName="Category.CategoryID"/>
<syncfusion:GridTextColumn HeaderText="Description" MappingName="Description" TextWrapping="Wrap" ColumnSizer="Auto"/>
<syncfusion:GridTextColumn HeaderText="HasCondition" MappingName="HasSalesCondition"/>
</syncfusion:SfDataGrid.Columns>
<syncfusion:SfDataGrid.DetailsViewDefinition>
<syncfusion:GridViewDefinition RelationalColumn="SelectedTieredCategoryMix">
<syncfusion:GridViewDefinition.DataGrid>
<syncfusion:SfDataGrid x:Name="SecondLevelNestedGrid1" AllowResizingColumns="True"
AutoGenerateColumns="False" AutoGenerateRelations="False" ColumnSizer="Auto">
<syncfusion:SfDataGrid.Columns>
<syncfusion:GridTextColumn HeaderText="Categories" MappingName="CategoryID"/>
</syncfusion:SfDataGrid.Columns>
</syncfusion:SfDataGrid>
</syncfusion:GridViewDefinition.DataGrid>
</syncfusion:GridViewDefinition>
<syncfusion:GridViewDefinition RelationalColumn="Tiers">
<syncfusion:GridViewDefinition.DataGrid>
<syncfusion:SfDataGrid x:Name="SecondLevelNestedGrid2" AllowResizingColumns="True"
AutoGenerateColumns="False" AutoGenerateRelations="False" ColumnSizer="Auto">
<syncfusion:SfDataGrid.Columns>
<syncfusion:GridTextColumn HeaderText="Tier" MappingName="Tier"/>
<syncfusion:GridTextColumn HeaderText="Min" MappingName="TierMinStr"/>
<syncfusion:GridTextColumn HeaderText="Max" MappingName="TierMaxStr"/>
<syncfusion:GridTextColumn HeaderText="Value" MappingName="ValueStr" />
</syncfusion:SfDataGrid.Columns>
</syncfusion:SfDataGrid>
</syncfusion:GridViewDefinition.DataGrid>
</syncfusion:GridViewDefinition>
</syncfusion:SfDataGrid.DetailsViewDefinition>
</syncfusion:SfDataGrid>
</syncfusion:GridViewDefinition.DataGrid>
</syncfusion:GridViewDefinition>
</syncfusion:SfDataGrid.DetailsViewDefinition>
</syncfusion:SfDataGrid>
If I remove GridViewDefinition with RelationalColumn="SelectedTieredCategoryMix" or GridViewDefinition with RelationalColumn="Tiers" than the export function works. Having both in there at the
same time causes the NullReferenceException. I am using version 15.2.0.40. The stack trace is attached.
Attachment:
stackTrace_2ac17ed4.rar