Excel Export - Specified argument was out of the range of valid values

6 posts, 1 answers
  1. Bill Wolff
    Bill Wolff avatar
    10 posts
    Member since:
    Jul 2006

    Posted 14 Jan 2015 Link to this post

    I am getting this error when a user clicks Export on the radGrid heading:

    Specified argument was out of the range of valid values.
    Parameter name: index

    [ArgumentOutOfRangeException: Specified argument was out of the range of valid values.
    Parameter name: index]
    System.Web.UI.ControlCollection.get_Item(Int32 index) +11120470
    Telerik.Web.UI.GridGroupPanel.get_MainTable() +54
    Telerik.Web.UI.GridGroupPanel.OnPreRender(EventArgs e) +56

    Any ideas? This also happens for PDF and Word exports. The grid uses an ObjectDataSource with EnablePaging true and a SortParameterName. Other grid functions like sorting, filtering, paging, and grouping work as advertised. The exports are setup in the markup like this:

        <ExportSettings Excel-Format="Html" Word-Format="Html" FileName="Batch"
            IgnorePaging="true" HideStructureColumns="true" OpenInNewWindow="true" ExportOnlyData="true">
  2. Kostadin
    Admin
    Kostadin avatar
    1713 posts

    Posted 19 Jan 2015 Link to this post

    Hello Bill,

    I am afraid that based on the provided information will be hard to pinpoint the reason for that behavior. Could you please provide your code declaration and the related code behind in order to investigate it further? Additionally could you disable the grouping and check out whether the issue remains?

    Regards,
    Kostadin
    Telerik
     

    Check out the Telerik Platform - the only platform that combines a rich set of UI tools with powerful cloud services to develop web, hybrid and native mobile apps.

     
  3. UI for ASP.NET Ajax is Ready for VS 2017
  4. Bill Wolff
    Bill Wolff avatar
    10 posts
    Member since:
    Jul 2006

    Posted 19 Jan 2015 in reply to Kostadin Link to this post

    I did some detailed testing and know what the problem is. I am using an ObjectDataSource with EnablePaging="true" and a SortParameterName. This provide the best performance for SQL queries which is my case are all parameterized stored procedures. When RadGrid does paging, it passes the PageSize as the maximumRows and the PageSize * PageIndex as the startRowIndex as expected. This works well.

    However, when you apply a filter to a column, RadGrid passes 2147483647 as the maximumRows which is the highest value for an integer. This sort of makes sense, and it is easy to check for this in a stored procedure and return all rows. The entire table is returned and RadGrid uses a LINQ expression on the row collection to display the filtered rows.

    The next problem was export to Excel, PDF, and Word. Oddly enough, when RadGrid does an export, it passes 0 as the maximumRows which my stored procedure was interpreting as no rows. I added a test for 0 to the stored proc and now return all rows so the export is working once again. If a filter is applied, the LINQ expression is honored and the export has only the filtered dat in it.

    I still can't get export to work at all with ShowGroupPanel="True". Any clues?







  5. Bill Wolff
    Bill Wolff avatar
    10 posts
    Member since:
    Jul 2006

    Posted 19 Jan 2015 Link to this post

    I did some more testing. If you set ShowGroupPanel="True" to enable grouping, any export button still causes the Index out of range error. The ObjectDataSource is called with 0 as the maximumRows and 0 as the startPageIndex. This should return all possible rows.The error could be in the filter LINQ expression but the same error occurs with and without a filter. There is something in your export code that gets confused by the grouping.

    Is there a simple way to turn off grouping prior to export but have it still work for the user?
  6. Answer
    Kostadin
    Admin
    Kostadin avatar
    1713 posts

    Posted 22 Jan 2015 Link to this post

    Hello Bill,

    A possible solution is to disable the grouping when export to Excel command is fired. This way the grouping will be removed only the export file and will not effect the rendered RadGrid. If you are using the built-in buttons then you can hook OnItemCommand event handler and disable the grouping as demonstrated below.
    protected void RadGrid1_ItemCommand(object sender, Telerik.Web.UI.GridCommandEventArgs e)
    {
        if (e.CommandName == RadGrid.ExportToExcelCommandName)
        {
            RadGrid1.MasterTableView.GroupByExpressions.Clear();
            if (!RadGrid1.ExportSettings.IgnorePaging)
                RadGrid1.Rebind();
        }
    }


    Regards,
    Kostadin
    Telerik
     

    Check out the Telerik Platform - the only platform that combines a rich set of UI tools with powerful cloud services to develop web, hybrid and native mobile apps.

     
  7. Bill Wolff
    Bill Wolff avatar
    10 posts
    Member since:
    Jul 2006

    Posted 22 Jan 2015 in reply to Kostadin Link to this post

    That helped and we can mark this one as answered. I had to set 

    RadGrid1.ShowGroupPanel = false; 

    instead of 

    RadGrid1.MasterTableView.GroupByExpressions.Clear()

    in the code above.
Back to Top
UI for ASP.NET Ajax is Ready for VS 2017