Here is my environment Specs:
- ASP.NET 2.0
- Microsoft Windows XP Professional Version 2002 SP2
- IE V 7
- Telerik Q2 2008
- 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> |
<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 |
} |
} |