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

When Clicking the page arrorow or pg Number, Index Out of Range Error returns

1 Answer 95 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Eric
Top achievements
Rank 1
Eric asked on 25 Nov 2008, 05:53 PM

Here is my environment Specs:

  1. ASP.NET 2.0
  2. Microsoft Windows XP Professional Version 2002 SP2
  3. IE V 7
  4. Telerik Q2 2008 
  5. C#

I have a radgrid that has more than one page of data to show, the data on the page is being filled correctly, and I am limiting it to 20 records per page. I am using both the numbers href links to page through the grid and the arrow, as more data is available to be shown. When I click on the page number or arrow to page through the data in the rad grid I get the following error (also in green where it happens on my code behind snippet down below):

Index was out of range. Must be non-negative and less than the size of the collection. Parameter name: index

My debug triggers the error on the codebehind on this line, in the rgUsers_ItemCommand() when I try to page to the next 20 records:
string sUserName = e.Item.OwnerTableView.DataKeyValues[e.Item.ItemIndex]["UserName"].ToString();

Do I need to do a conditional check to make sure that the rgUsers table has a row count greater than zero? Or is there some property I am not using to allow this? My dataset returns 30 users so I expect to see at least 10 users on page 2 of the radgrid.

Code from the aspx Page:

 

 

 

<telerik:RadGrid ID="rgUsers" Skin="Office2007" runat="server" AllowPaging="True" 
                        PageSize="20" AllowSorting="True" Width="100%" AutoGenerateColumns="False" OnNeedDataSource="rgUsers_OnNeedDataSource" 
                        OnPreRender="rgUsers_PreRender" OnDeleteCommand="rgUsers_DeleteCommand" OnItemCommand="rgUsers_ItemCommand" GridLines="None">  
                        <PagerStyle Mode="NextPrevAndNumeric"></PagerStyle> 
                        <MasterTableView CommandItemDisplay="None" GridLines="None" DataKeyNames="UserName">  
                            <RowIndicatorColumn Visible="False">  
                                <HeaderStyle Width="20px" /> 
                            </RowIndicatorColumn> 
                            <ExpandCollapseColumn Visible="False">  
                                <HeaderStyle Width="19px" /> 
                            </ExpandCollapseColumn> 
                            <Columns> 
                                <telerik:GridEditCommandColumn> 
                                </telerik:GridEditCommandColumn> 
                                <telerik:GridBoundColumn DataField="UserName" HeaderText="UserName" UniqueName="UserName">  
                                </telerik:GridBoundColumn> 
                                <telerik:GridBoundColumn DataField="Email" HeaderText="Email" UniqueName="Email">  
                                    <HeaderStyle Width="25%" /> 
                                </telerik:GridBoundColumn> 
                                <telerik:GridBoundColumn DataField="LastActivityDate" HeaderText="Last Activity Date" 
                                    UniqueName="LastActivityDate">  
                                    <HeaderStyle Width="15%" /> 
                                </telerik:GridBoundColumn> 
                                <telerik:GridCheckBoxColumn DataField="IsApproved" HeaderText="Approved" UniqueName="IsApproved">  
                                    <HeaderStyle Width="12%" /> 
                                </telerik:GridCheckBoxColumn> 
                                <telerik:GridCheckBoxColumn DataField="IsLockedOut" HeaderText="Locked Out" UniqueName="IsLockedOut">  
                                    <HeaderStyle Width="12%" /> 
                                </telerik:GridCheckBoxColumn> 
                                <telerik:GridTemplateColumn UniqueName="TemplateColumn1">  
                                    <ItemTemplate> 
                                        &nbsp;<asp:LinkButton ID="lnkBtnDelete" runat="server" CausesValidation="false" CommandName="Delete" 
                                            ForeColor="#044A7E" Text="Delete" Visible='<%# DelButShowHide((DataBinder.Eval (Container.DataItem, "UserName").ToString())) %>'></asp:LinkButton> 
                                    </ItemTemplate> 
                                    <HeaderStyle Width="8%" /> 
                                </telerik:GridTemplateColumn> 
                            </Columns> 
                            <EditFormSettings> 
                                <EditColumn UniqueName="EditCommandColumn1">  
                                </EditColumn> 
                            </EditFormSettings> 
                        </MasterTableView> 
                        <HeaderStyle HorizontalAlign="Left" /> 
                        <FilterMenu EnableTheming="True" Skin="Office2007">  
                            <CollapseAnimation Duration="200" Type="OutQuint" /> 
                        </FilterMenu> 
                        <SortingSettings SortedAscToolTip="/Manager/images/arrw_up.gif" SortedDescToolTip="/Manager/images/arrw_d.gif" /> 
                    </telerik:RadGrid> 

 

 

Code from the aspx.cs code behind:

 

 

 

protected void rgUsers_ItemCommand(object source, GridCommandEventArgs e)  
    {  
 
        //Errors out on this line :Index was out of range. Must be non-negative and less than the size of the collection. Parameter name: index  
         string sUserName = e.Item.OwnerTableView.DataKeyValues[e.Item.ItemIndex]["UserName"].ToString();  
        //End Error out on this line  
 
        MembershipUser oUser = Membership.GetUser(sUserName);  
        DisplayMessage(string.Empty);  
 
        if (e.CommandName == RadGrid.EditCommandName)//Edit a user  
        {  
            e.Canceled = true;//interrupts the rgUsers commands so that it does not show inline edit forms  
              
            hdnSubmitType.Value = "UpdateUser";  
            PageMode = mode.form;  
            RolePrivPanelMode = RolePrivMode.roleChoice; //sets default to role selected  
            tbUserName.Enabled = false;  
            hydrateEditForm(oUser);  
            buildRadTrees(UserInfo.UserIdentityKey.ToString(), oUser.ProviderUserKey.ToString());  
              
            UserInfo.UserIdentityKey.ToString());  
            CheckedRadTreesNodes(oUser);  
            RolePrivPanelMode = RolePrivMode.roleChoice; //set Roles panel to be shown  
 
        }  
    } 

 

 

 

 

 

1 Answer, 1 is accepted

Sort by
0
Eric
Top achievements
Rank 1
answered on 25 Nov 2008, 09:31 PM
Wow, I must be slow of mind. I figured out my issue.

I just moved the sUserName declaration into the if, and it works.

Please close or delete this thread.
Tags
Grid
Asked by
Eric
Top achievements
Rank 1
Answers by
Eric
Top achievements
Rank 1
Share this question
or