Click here to Skip to main content
Rate this: bad
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") %>' >
                        <asp:BoundField DataField ="UploadDate" HeaderText="UploadDate" SortExpression="UploadDate" />
                                                <%--<asp:ImageButton ID="imgCPTICD" runat ="server" ImageUrl="~/Images/Plus.JPG" CommandName="Show" CommandArgument='<%# Container.DataItemIndex%>' />--%>
                        <asp:ImageButton ID="imgCPTICD" runat ="server"  ImageUrl="~/Images/Plus.JPG" CommandName="Show" CommandArgument='<%# DataBinder.Eval(Container,"DataItemIndex") %>' />
                        <td colspan="6">
                        <asp:GridView ID="grvCPTICD" runat ="server" AutoGenerateColumns="false" HeaderStyle-BackColor="#3399FF" HeaderStyle-ForeColor="White">
                <HeaderStyle Font-Bold="True" Font-Underline="False" ForeColor="#0033CC" 
                    BackColor="#6699FF" Font-Names="Trebuchet MS" Font-Size="12px" />


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> //Here i am getting the error while using paging, without paging it is coming correct.
                if (imgBtn.ImageUrl == "~/Images/Plus.JPG")
                    Label lblAHSL = (Label)grvRecords.Rows[RowIndex].FindControl("lbliPatientId");
                    GridView grvSub = (GridView)grvRecords.Rows[RowIndex].FindControl("grvCPTICD");
                    BindInnerGridView(grvSub, Convert.ToInt32(lblAHSL.Text));
                    imgBtn.ImageUrl = "~/Images/Minus.JPG";
                else if (imgBtn.ImageUrl == "~/Images/Minus.JPG")
                    GridView grvSub = (GridView)grvRecords.Rows[RowIndex].FindControl("grvCPTICD");
                    grvSub.DataSource = null;
                    grvSub.EmptyDataText = "";
                    imgBtn.ImageUrl = "~/Images/Plus.JPG";
        catch (Exception ex)
            lblMsg.Visible = true;
            lblMsg.Text = ex.Message;
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 2-Jul-12 0:21am
Edited 2-Jul-12 1:14am
Arjun YK at 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 at 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 at 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
0 OriginalGriff 445
1 Suvendu Shekhar Giri 134
2 Abhinav S 126
3 Maciej Los 125
4 Peter Leow 120
0 Sergey Alexandrovich Kryukov 9,623
1 OriginalGriff 8,870
2 Peter Leow 4,984
3 Kornfeld Eliyahu Peter 3,300
4 Maciej Los 2,456

Advertise | Privacy | Mobile
Web04 | 2.8.150327.1 | Last Updated 2 Jul 2012
Copyright © CodeProject, 1999-2015
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