In Development
Last Updated: 07 Feb 2019 09:36 by ADMIN
Keith
Created on: 25 Jan 2019 15:11
Category: Grid
Type: Bug Report
0
Export an empty RadGrid with caption set throws exception index was out of range
Exporting an empty RadGrid to XLSX or Biff with ExportSettings-ExportOnlyData="true" and Caption set, throws exception: Index was out of range. Must be non-negative and less than the size of the collection.
Parameter name: index

Steps to reproduce:

RadGrid markup:

<telerik:RadGrid ID="RadGrid1" runat="server" AutoGenerateColumns="true"
    OnNeedDataSource="RadGrid1_NeedDataSource">
    <ExportSettings ExportOnlyData="true">
        <Excel Format="Biff" />
    </ExportSettings>
    <MasterTableView Caption="My Personalized caption" CommandItemDisplay="Top">
        <CommandItemSettings ShowExportToExcelButton="true" />
    </MasterTableView>
</telerik:RadGrid>


C# - Code behind

protected void RadGrid1_NeedDataSource(object sender, GridNeedDataSourceEventArgs e)
{
    RadGrid1.DataSource = OrdersTable();
}
 
private DataTable OrdersTable()
{
    DataTable dt = new DataTable();
    dt.Columns.Add(new DataColumn("OrderID", typeof(int)));
    dt.Columns.Add(new DataColumn("OrderDate", typeof(DateTime)));
    dt.Columns.Add(new DataColumn("Freight", typeof(decimal)));
    dt.Columns.Add(new DataColumn("ShipName", typeof(string)));
    dt.Columns.Add(new DataColumn("ShipCountry", typeof(string)));
    dt.PrimaryKey = new DataColumn[] { dt.Columns["OrderID"] };
    return dt;
}
1 comment
ADMIN
Attila Antal
Posted on: 25 Jan 2019 15:30
While a fix is delivered, a viable workaround could be to remove the caption while exporting.

Wire up the grid to its PreRender event:

<telerik:RadGrid ID="RadGrid1" runat="server" AutoGenerateColumns="true"
    OnPreRender="RadGrid1_PreRender"
    OnNeedDataSource="RadGrid1_NeedDataSource">
    <ExportSettings ExportOnlyData="true">
        <Excel Format="Biff" />
    </ExportSettings>
    <MasterTableView Caption="My Personalized caption" CommandItemDisplay="Top">
        <CommandItemSettings ShowExportToExcelButton="true" />
    </MasterTableView>
</telerik:RadGrid>


In the PreRender event handler, set the condition if the grid is exporting, clear the custom caption.

protected void RadGrid1_PreRender(object sender, EventArgs e)
{
    if (RadGrid1.IsExporting)
    {
        RadGrid1.MasterTableView.Caption = string.Empty;
    }
}