This is a migrated thread and some comments may be shown as answers.

Trouble with Paging after Sorting

6 Answers 190 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Peter
Top achievements
Rank 1
Peter asked on 30 Jun 2009, 12:09 PM

The Grid works fine unless the User sorts the Results. As soon as one of the Columns is sorted by, the Grid seems to lose all the Data. If I page to the next set of Data it gives an "Index Out Of Range" Exception because no DataRows are present (even thought well over 300 Records are set as DataSource during the NeedDataSource Event).

Any Ideas? ViewState is enabled, and paging does work correctly before sorting.

Here is the Setup of the Grid:

<telerik:RadGrid ID="rgResult" runat="server"   
                    meta:resourcekey="radGrid" 
                    AutoGenerateColumns="False" 
                    skin="<%# Skin %>" GridLines="None"   
                    onneeddatasource="rgResult_NeedDataSource" 
                    onitemcommand="rgResult_ItemCommand"   
                    OnDetailTableDataBind="rgResult_DetailTableDataBind" 
                    AllowPaging="True" PageSize="15" Allowsorting="true"

6 Answers, 1 is accepted

Sort by
0
Peter
Top achievements
Rank 1
answered on 30 Jun 2009, 01:33 PM
[IndexOutOfRangeException: Der Index 15 ist entweder negativ oder über der Gesamtanzahl der Zeilen.]  
   System.Data.DataView.GetRow(Int32 index) +4815675  
   System.Data.DataView.get_Item(Int32 recordIndex) +12  
   Telerik.Web.UI.GridEnumerableFromDataView.PerformTransformation() +5489  
   Telerik.Web.UI.GridEnumerableFromDataView.TransformEnumerable() +41  
   Telerik.Web.UI.GridTableView.GetEnumerator(Boolean useDataSource, GridEnumerableBase resolvedDataSource, ArrayList dataKeysArray) +171  
   Telerik.Web.UI.GridTableView.CreateControlHierarchy(Boolean useDataSource) +356  
   Telerik.Web.UI.GridTableView.CreateChildControls(IEnumerable dataSource, Boolean useDataSource) +636  
   System.Web.UI.WebControls.CompositeDataBoundControl.PerformDataBinding(IEnumerable data) +57  
   System.Web.UI.WebControls.DataBoundControl.OnDataSourceViewSelectCallback(IEnumerable data) +114  
   System.Web.UI.DataSourceView.Select(DataSourceSelectArguments arguments, DataSourceViewSelectCallback callback) +31  
   System.Web.UI.WebControls.DataBoundControl.PerformSelect() +142  
   Telerik.Web.UI.GridTableView.PerformSelect() +28  
   System.Web.UI.WebControls.BaseDataBoundControl.DataBind() +73  
   Telerik.Web.UI.GridTableView.DataBind() +368  
   Telerik.Web.UI.GridPageChangedEventArgs.ExecuteCommand(Object source) +2703  
   Telerik.Web.UI.RadGrid.OnBubbleEvent(Object source, EventArgs e) +191  
   System.Web.UI.Control.RaiseBubbleEvent(Object source, EventArgs args) +37  
   Telerik.Web.UI.GridItem.OnBubbleEvent(Object source, EventArgs e) +61  
   System.Web.UI.Control.RaiseBubbleEvent(Object source, EventArgs args) +37  
   Telerik.Web.UI.GridItem.OnBubbleEvent(Object source, EventArgs e) +165  
   Telerik.Web.UI.GridItem.FireCommandEvent(String commandName, Object commandArgument) +80  
   Telerik.Web.UI.RadGrid.RaisePostBackEvent(String eventArgument) +4247  
   System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument) +13  
   System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) +175  
   System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +1565 


This is the Exception that I get.
0
Pavlina
Telerik team
answered on 01 Jul 2009, 03:54 PM
Hi Peter,

I am afraid that the provided information is not enough to determine the exact source of the issue you are facing. In order to find a quick solution/fix of this problem I will ask you to send us a small example project via support ticket or at least the problematic page. Thus we could do our best to help you in resolving it.

Greetings,
Pavlina
the Telerik team

Instantly find answers to your questions on the new Telerik Support Portal.
Check out the tips for optimizing your support resource searches.
0
Peter
Top achievements
Rank 1
answered on 02 Jul 2009, 06:21 AM
I'm sorry. I was hoping this would be an issue someone else has encountered already.

As a first step I am going to post the Grid (aspx) and codebehind, the complex project doesn't allow me to extract the Grid with the same functionality easily.

Here is the entire Grid:

<telerik:RadGrid ID="rgResult" runat="server"   
                    meta:resourcekey="radGrid" 
                    AutoGenerateColumns="False" 
                    skin="<%# Skin %>" GridLines="None"   
                    onneeddatasource="rgResult_NeedDataSource" 
                    onitemcommand="rgResult_ItemCommand"   
                    OnDetailTableDataBind="rgResult_DetailTableDataBind" 
                    AllowPaging="True" PageSize="15" Allowsorting="true">  
                <SelectedItemStyle CssClass="MySelectedClass"    /> 
                <ValidationSettings EnableValidation="false" /> 
                <ExportSettings IgnorePaging="true" OpenInNewWindow="true">  
                    <Pdf AllowAdd="false" AllowCopy="true" AllowModify="true" AllowPrinting="true" Author="Anonymous" 
                        Keywords="None" PageTitle="RadGrid export document" 
                        Subject="RadGrid Export" Title="RadGrid export"/>  
                    <Excel Format="Html"></Excel> 
                    <Csv RowDelimiter="NewLine" ColumnDelimiter="Comma"></Csv> 
                </ExportSettings>   
                      
                <HierarchySettings />   
                 
                <MasterTableView Name="results" DataKeyNames="Id">  
                    <PagerTemplate> 
                        <table width="100%">  
                            <tr> 
                                <td> 
                                    <asp:ImageButton ImageUrl="~/Img/first.gif" ID="btnFirst" runat="server" OnClientClick="changePage('first'); return false;" 
                                        CommandName="Page" CommandArgument="First" /> 
                                </td> 
                                <td> 
                                    <asp:ImageButton ImageUrl="~/Img/prev.gif" ID="btnPrev" runat="server" OnClientClick="changePage('prev'); return false;" 
                                        CommandName="Page" CommandArgument="Prev" ValidationGroup="pagerValidation" /> 
                                </td> 
                                <td> 
                                    <asp:Label runat="server" meta:resourcekey="lblPage" /> 
                                    <asp:TextBox ID="tbPageNumber" runat="server" Columns="3" Text='<%# (int)DataBinder.Eval(Container, "OwnerTableView.CurrentPageIndex") + 1 %>'/>  
                                    <asp:RangeValidator runat="Server" ID="RangeValidator1" ControlToValidate="tbPageNumber"   
                                        EnableClientScript="true" MinimumValue="1" Type="Integer" MaximumValue='<%# DataBinder.Eval(Container, "Paging.PageCount") %>' 
                                        ErrorMessage='<%# GetLocalResourceObject("resultPagerValidationMessage") + " 1 - " + DataBinder.Eval(Container, "Paging.PageCount") %>' 
                                        Display="Dynamic" ValidationGroup="pagerValidation"></asp:RangeValidator> 
                                    <asp:LinkButton ID="btnGo" runat="server" CommandName="CustomChangePage" meta:resourcekey="btnGo" ValidationGroup="pagerValidation"></asp:LinkButton> 
                                </td> 
                                <td> 
                                    <asp:ImageButton ImageUrl="~/Img/next.gif" ID="btnNext" runat="server" OnClientClick="changePage('next'); return false;" 
                                        CommandName="Page" CommandArgument="Next" ValidationGroup="pagerValidation" /> 
                                </td> 
                                <td> 
                                    <asp:ImageButton ImageUrl="~/Img/last.gif" ID="btnLast" runat="server" OnClientClick="changePage('last'); return false;" 
                                        CommandName="Page" CommandArgument="Last"/>  
                                </td> 
                                <td> 
                                    <asp:Panel runat="server" ID="NumericPagerPlaceHolder"</asp:Panel> 
                                </td> 
                                <td align="right">  
                                    <asp:Label runat="server" meta:resourcekey="lblPageFrom"></asp:Label> 
                                    <%# (int)DataBinder.Eval(Container, "Paging.CurrentPageIndex") + 1 %> 
                                    <asp:Label ID="Label1" runat="server" meta:resourcekey="lblPageOf"></asp:Label> 
                                    <%# DataBinder.Eval(Container, "Paging.PageCount")%>,  
                                    <asp:Label ID="Label2" runat="server" meta:resourcekey="lblElementsFrom"></asp:Label> 
                                    <%# (int)DataBinder.Eval(Container, "Paging.FirstIndexInPage") + 1 %> 
                                    <asp:Label ID="Label3" runat="server" meta:resourcekey="lblElementsTo"></asp:Label> 
                                    <%# (int)DataBinder.Eval(Container, "Paging.LastIndexInPage") + 1 %> 
                                    <asp:Label ID="Label4" runat="server" meta:resourcekey="lblPageOf"></asp:Label> 
                                    <%# DataBinder.Eval(Container, "Paging.DataSourceCount")%>.  
                                </td> 
                            </tr> 
                        </table> 
                    </PagerTemplate>            
                                
                    <PagerStyle Mode="NumericPages" PageButtonCount="10" AlwaysVisible="true"/>  
                    <Columns> 
                        <telerik:GridClientSelectColumn Reorderable="False" UniqueName="rgResultClientSelectColumn">  
                            <HeaderStyle Width="20px"></HeaderStyle> 
                        </telerik:GridClientSelectColumn> 
 
                          
                        <telerik:GridBoundColumn UniqueName="Name" 
                            meta:resourcekey="colResultName" 
                            DataField="Name">  
                            <HeaderStyle Width="125px"></HeaderStyle> 
                        </telerik:GridBoundColumn> 
                          
                        <telerik:GridBoundColumn UniqueName="Description" 
                            meta:resourcekey="colResultDescription" 
                            DataField="Description">  
                            <HeaderStyle Width="125px"></HeaderStyle> 
                        </telerik:GridBoundColumn> 
                          
                        <telerik:GridButtonColumn   
                            meta:resourcekey="gridGoToColumn" 
                            CommandName="GoTo"   
                            UniqueName="gotoColumn" 
                            ButtonType="ImageButton">  
                            <HeaderStyle Width="30px" /> 
                        </telerik:GridButtonColumn> 
                    </Columns> 
                    <DetailTables > 
                        <telerik:GridTableView   
                                meta:resourcekey="mytable" 
                                Name="ResultAttributes" 
                                Width="600px" 
                                AutoGenerateColumns="false">  
                            <Columns> 
                                <telerik:GridBoundColumn UniqueName="name"   
                                    meta:resourcekey="colresultAttributeName" 
                                    DataField="Attribute.Name">  
                                    <HeaderStyle Width="125px"></HeaderStyle> 
                                </telerik:GridBoundColumn> 
                                  
                                <telerik:GridBoundColumn UniqueName="value"   
                                  meta:resourcekey="colresultAttributeDescription" 
                                    DataField="Value">  
                                    <HeaderStyle Width="125px"></HeaderStyle> 
                                </telerik:GridBoundColumn> 
                            </Columns> 
                        </telerik:GridTableView> 
                    </DetailTables> 
                </MasterTableView> 
                <ClientSettings> 
                    <Selecting AllowRowSelect="True"></Selecting> 
                </ClientSettings> 
            </telerik:RadGrid> 

It has a written over PagerTemplate which I thought could be the cause, but everything seems to be OK with it. Keep in mind, it does page successfully before sorting!

This is the codebehind on ItemCommand. "GoTo" Is not relevant to this, but if I page via CustomChangePage the line that the exception gets thrown at is "e.Item.OwnerTableView.Rebind()". I debugged through the whole thing and the DataSource gets set correctly in the NeedDataSource event, however, the DataSource seems to be missing when the paging occurs (to be exact only the first page is present, if I click to the second page the exception comes, if I click to subsequent pages "No Data" is displayed in the Grid).

protected void rgResult_ItemCommand(object source, GridCommandEventArgs e)  
        {  
            if (e.CommandName.Equals("GoTo"))  
            {  
                string id = (e.Item as GridDataItem).GetDataKeyValue("Id").ToString();  
                switch (this.SearchType)  
                {  
                    case MaintosEnums.SearchType.MasterdataObjectSearch:  
                        Response.Redirect("~/SecuredSites/Masterdata.aspx?open=open&Id=" + id);  
                        break;  
                    case MaintosEnums.SearchType.TypeSearch:  
                        Response.Redirect("~/ParamPage.aspx?Id=" + id + "&" + Parameterization.PARAM_OPEN + "=" + id + "&type=Type");  
                        break;  
                    case MaintosEnums.SearchType.MaintenanceSearch:  
                        string mdid = "";  
                        foreach (ISearchable s in this.SearchResult)  
                            if (s is IActivityInstance)  
                                if(s.Id.Equals(id))  
                                    mdid = (s as IActivityInstance).MasterdataObject.Id;  
                        Response.Redirect("~/Maintenance/MaintenanceProtocol.aspx?ActivityInstanceId=" + id + "&" + Operational1.NODE2OPEN + "=" + mdid);  
                        break;  
                    case MaintosEnums.SearchType.OccurrenceSearch:  
                        Response.Redirect("~/Occurrence/InitialProtocol.aspx?Id=" + id);  
                        break;  
                }  
            }  
            else if (e.CommandName.Equals("CustomChangePage"))  
            {  
                string newPageIndexString = (e.Item.FindControl("tbPageNumber"as TextBox).Text;  
                int newPageIndex = e.Item.OwnerTableView.CurrentPageIndex;  
                if (newPageIndexString.Trim() != "")  
                {  
                    try 
                    {  
                        newPageIndex = int.Parse(newPageIndexString) - 1;  
                    }  
                    catch 
                    {  
                        //process incorrect input here if needed  
                    };  
                }  
                if (e.Item.OwnerTableView.CurrentPageIndex != newPageIndex)  
                {  
                    e.Item.OwnerTableView.CurrentPageIndex = newPageIndex;  
                    e.Item.OwnerTableView.Rebind();  
                }  
            }  
        } 

The other codebehind methods are pretty basic:
        protected void rgResult_NeedDataSource(object source, GridNeedDataSourceEventArgs e)  
        {  
            (source as RadGrid).DataSource = this.SearchResult;  
        }  
 
protected void rg_ItemDataBound(object sender, GridItemEventArgs e)  
        {  
            if (e.Item is GridDataItem)  
            {  
                GridDataItem item = (GridDataItem)e.Item;  
                ImageButton button = (ImageButton)item["DeleteColumn"].Controls[0];  
                button.Attributes["onclick"] = "return confirm('" + this.GetLocalResourceObject("doYouWantToDelete") + "')";  
 
            }  
        }  
 
        protected void rgResult_PreRender(object sender, EventArgs e)  
        {  
            try 
            {  
                if (this.AllowResultSelection)  
                {  
                    rgResult.MasterTableView.GetColumn("rgResultClientSelectColumn").Visible = true;  
                    rgResult.AllowMultiRowSelection = true;  
                }  
                else 
                {  
                    rgResult.MasterTableView.GetColumn("rgResultClientSelectColumn").Visible = false;  
                }  
            }  
            catch (Exception ex)  
            {  
                logger.Error("Error while prerendering rgResult", ex);  
                throw;  
            }  
        }  
        protected void rgResult_DetailTableDataBind(object source, GridDetailTableDataBindEventArgs e)  
        {  
            try 
            {  
                if (this.NeedAttributesForResult != null)  
                {  
                    NeedAttributesForResultEventArgs args =  
                        new NeedAttributesForResultEventArgs  
                            (e.DetailTableView.ParentItem.GetDataKeyValue("Id").ToString(),  
                            this.SearchType);  
                    this.NeedAttributesForResult(source, args);  
                    e.DetailTableView.DataSource = args.Attributes;  
                }  
            }  
            catch (Exception ex)  
            {  
                logger.Error("Error while detail table databind", ex);  
                throw;  
            }  
        } 

I want to stress that the Exception occurs only when paging and sorting are combined. The two scenarios to produce the Exception are: 1.) Sorting the Grid on the first page and then paging to second page.
2.) Paging to the second page, and then sorting.

What seems to be the case is that when the Data is sorted only the first 15 records are present (first page). If I page to the third page and sort no records are displayed, but when I take off the sorting the records reappear. The question is: What happens with the Data and why are the records not displayed?
0
Peter
Top achievements
Rank 1
answered on 02 Jul 2009, 06:27 AM
Thank you very much for the support!

Would it make sense for me to rework the sorting mechanism? I can write over the OnSorting Command and let C# do the work to sort my records before the grid gets them.
0
Accepted
Helmut Stelczenmayr
Top achievements
Rank 1
answered on 06 Jul 2009, 11:28 AM
After some more searching on this topic I found that this bug had already been fixed. I was using an older build.

So. If anyone is experiencing this try updating your DLL for the newest version available!
0
Pavlina
Telerik team
answered on 06 Jul 2009, 04:08 PM
Hi Helmut,

Thank you for sharing your solution in this public forum thread. Thus you could help other people which have a similar problem.
Please do not hesitate to contact us if you encounter any problems,.

Kind regards,
Pavlina
the Telerik team

Instantly find answers to your questions on the new Telerik Support Portal.
Check out the tips for optimizing your support resource searches.
Tags
Grid
Asked by
Peter
Top achievements
Rank 1
Answers by
Peter
Top achievements
Rank 1
Pavlina
Telerik team
Helmut Stelczenmayr
Top achievements
Rank 1
Share this question
or