Click here to Skip to main content
12,401,049 members (61,425 online)
Rate this:
Please Sign up or sign in to vote.
See more: ASP.NET Gridview paging
Hi all,

I have a gridview, in that, row databound event is working fine, but when i am using gridview paging, its says the error "
index was out of range. must be non-negative and less than the size of the collection.

Gridview design

<asp:GridView ID="grvRecords" runat ="server" AutoGenerateColumns="false" Font-Bold="True"
                        Font-Names="Trebuchet MS" Font-Size="10px" AllowPaging="True" 
                    Font-Underline="False" onrowcommand="grvRecords_RowCommand" 
                    onpageindexchanging="grvRecords_PageIndexChanging" AllowSorting="True" 
                    onsorting="grvRecords_Sorting" PageSize="5" DataKeyNames="iPatientId,iCodingId" >
            <asp:TemplateField  >
                        <HeaderTemplate >
                       <input id="chkAllItems" type="checkbox"   önclick="CheckAllDataGridCheckBoxes('chkItemChecked',document.forms[0].chkAllItems.checked)" value="Check">
                        <asp:CheckBox id="chkBulk" runat="server" Width="30px" AutoPostBack="False"> 
                           <asp:TemplateField Visible="false"  >
                        <asp:Label ID="lblCodingId" runat ="server" Text='<%#Eval("iCodingId") %>'  >
                        <asp:TemplateField HeaderText="AHSL#" SortExpression="iPatientId" >
                        <asp:Label ID="lbliPatientId" runat ="server" Text='<%#Eval("iPatientId") %>' >

In row-DataBound event

protected void grvRecords_RowCommand(object sender, GridViewCommandEventArgs e)
            if (e.CommandName == "Show")
                int RowIndex = Convert.ToInt32((e.CommandArgument).ToString());
                <big>ImageButton imgBtn = (ImageButton)grvRecords.Rows[RowIndex].FindControl("imgCPTICD");</big>

This is my pageIndex change code

protected void grvRecords_PageIndexChanging(object sender, GridViewPageEventArgs e)
        grvRecords.PageIndex = e.NewPageIndex;

can any one send the solution plz

Thanks in advance

Posted 1-Jul-12 23:21pm
Updated 2-Jul-12 0:14am
Arjun YK 2-Jul-12 5:50am
actually, even I've used the same code and facing the same problem. can anyone help us out?

1 solution

Rate this: bad
Please Sign up or sign in to vote.

Solution 1

Try updating you image button html somelike below

<asp:ImageButton runat="server" ID="imgCPTICD"
             ImageUrl="~/Images/Plus.JPG" CommandName="Show"

If you are using Index only for finding the Image button then you can replace the code like below

You can directly take the sender as the Show(+) button

Current Code

ImageButton imgBtn = (ImageButton)grvRecords.Rows[RowIndex].FindControl("imgCPTICD");

New Code

ImageButton imgBtn = (ImageButton)sender;
Murthy_NIt 2-Jul-12 6:09am
Unable to cast object of type 'System.Web.UI.WebControls.GridView' to type 'System.Web.UI.WebControls.ImageButton'.

Now i got this error
Murthy_NIt 2-Jul-12 6:13am
Its working not from ImageButton imgBtn = (ImageButton)sender;
Now i use this one, got fixed.
ImageButton imgBtn = (ImageButton)grvRecords.Rows[RowIndex].FindControl("imgCPTICD");

Thanks alot

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy | Mobile
Web02 | 2.8.160721.1 | Last Updated 2 Jul 2012
Copyright © CodeProject, 1999-2016
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100