I'm trying to export this data grid but running into Index was out of range exception.
What's causing this? Please help me fix this.
Just click the "Export to Excel" button to see the issue.
https://blazorplayground.syncfusion.com/VtByDvhcFoaoioEF
Unhandled exception rendering component: Index was out of range. Must be non-negative and less than the size of the collection. (Parameter 'index')
System.ArgumentOutOfRangeException: Index was out of range. Must be non-negative and less than the size of the collection. (Parameter 'index')
at System.Collections.Generic.List`1[[Syncfusion.ExcelExport.Cell, Syncfusion.ExcelExport.Net, Version=30.1.39.0, Culture=neutral, PublicKeyToken=3d67ed1f87d44c89]].get_Item(Int32 index)
at Syncfusion.ExcelExport.BaseCollection`1[[Syncfusion.ExcelExport.Cell, Syncfusion.ExcelExport.Net, Version=30.1.39.0, Culture=neutral, PublicKeyToken=3d67ed1f87d44c89]].get_Item(Int32 index)
at Syncfusion.Blazor.Grids.Internal.GridExcelExport`1[[Playground.User.DetailGrid.GridRowViewModelV2, Playground.User, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null]].GenerateStackedRows(GridColumn Col, Int32 RowIndex, Int32 ColDepth, List`1 SpannedCellIndex, Boolean isChildLevelStacked)
at Syncfusion.Blazor.Grids.Internal.GridExcelExport`1[[Playground.User.DetailGrid.GridRowViewModelV2, Playground.User, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null]].ProcessStackedHeader(List`1 Cols)
at Syncfusion.Blazor.Grids.Internal.GridExcelExport`1[[Playground.User.DetailGrid.GridRowViewModelV2, Playground.User, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null]].IterateElements()
at Syncfusion.Blazor.Grids.Internal.GridExcelExport`1[[Playground.User.DetailGrid.GridRowViewModelV2, Playground.User, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null]].ExportHandler()
at Syncfusion.Blazor.Grids.Internal.GridExcelExport`1.<ExecuteResult>d__154[[Playground.User.DetailGrid.GridRowViewModelV2, Playground.User, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null]].MoveNext()
at Syncfusion.Blazor.Grids.Internal.GridExcelExport`1.<ExportHelper>d__151[[Playground.User.DetailGrid.GridRowViewModelV2, Playground.User, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null]].MoveNext()
at Syncfusion.Blazor.Grids.Internal.GridExcelExport`1.<ExcelExport>d__152[[Playground.User.DetailGrid.GridRowViewModelV2, Playground.User, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null]].MoveNext()
at Syncfusion.Blazor.Grids.SfGrid`1.<ExportToExcelAsync>d__64[[Playground.User.DetailGrid.GridRowViewModelV2, Playground.User, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null]].MoveNext()
at Syncfusion.Blazor.Grids.SfGrid`1.<ExportToExcelAsync>d__63[[Playground.User.DetailGrid.GridRowViewModelV2, Playground.User, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null]].MoveNext()
at Playground.User.DetailGrid.ExportAsync()
at Microsoft.AspNetCore.Components.ComponentBase.CallStateHasChangedOnAsyncCompletion(Task task)
at Syncfusion.Blazor.Internal.SfBaseUtils.<InvokeEvent>d__12`1[[Microsoft.AspNetCore.Components.Web.MouseEventArgs, Microsoft.AspNetCore.Components.Web, Version=8.0.0.0, Culture=neutral, PublicKeyToken=adb9793829ddae60]].MoveNext()
at Syncfusion.Blazor.Buttons.SfButton.OnClickHandler(MouseEventArgs args)
at Microsoft.AspNetCore.Components.ComponentBase.CallStateHasChangedOnAsyncCompletion(Task task)
at Microsoft.AspNetCore.Components.RenderTree.Renderer.GetErrorHandledTask(Task taskToHandle, ComponentState owningComponentState)
Hi Ashish Khanal,
Sorry for the delay in getting back to you.
We’d like to inform you that when exporting template columns in the Blazor DataGrid to Excel, the user need to enable the IncludeTemplateColumn property within the ExcelExportProperties. This ensures that the template column is included in the exported Excel file, aligning with your requirement.
To render the exported content in a format similar to the UI, you can customize the output using the ExcelExportQueryCellInfoHandler event as like below.
Code Snippet:
|
private async Task ExportAsync() { try { if (Grid is null) return;
await Grid.ShowSpinnerAsync();
var exportProps = new Syncfusion.Blazor.Grids.ExcelExportProperties { FileName = "BorderIssueRepro.xlsx", IncludeTemplateColumn = true, …. } ….
public void ExcelExportQueryCellInfoHandler (Syncfusion.Blazor.Grids.ExcelQueryCellInfoEventArgs<GridRowViewModelV2> args) { var row = args.Data; var style = args.Style;
if (args.Column.Field == "Model" && (args.RowIndex % 2 == 0)) // to render excel as like in grid UI. { args.Cell.Value = string.Empty; } … |
Please check the above sample for more information and get back to us if you have any concerns.
Regards,
Sanjay Kumar Suresh
Hi Sanjay,
Thank you for the response.
Turns out, all I needed was this line, and it worked correctly.
IncludeTemplateColumn = true,
Thanks again!
Hi Ashish Khanal,
We are happy to hear that the provided solution was helpful. Please get back to us if you need any other assistance.
Regards,
Sanjay Kumar Suresh