Click here to Skip to main content
Rate this: bad
good
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" >
            <columns>
            <asp:TemplateField  >
                        <HeaderTemplate >
                       <input id="chkAllItems" type="checkbox"   önclick="CheckAllDataGridCheckBoxes('chkItemChecked',document.forms[0].chkAllItems.checked)" value="Check">
                        </HeaderTemplate>
                        <itemtemplate>
                        <asp:CheckBox id="chkBulk" runat="server" Width="30px" AutoPostBack="False"> 
                        </itemtemplate>
                        
                           <asp:TemplateField Visible="false"  >
                        <itemtemplate>
                        <asp:Label ID="lblCodingId" runat ="server" Text='<%#Eval("iCodingId") %>'  >
                        </itemtemplate>
                        
                          
                        <asp:TemplateField HeaderText="AHSL#" SortExpression="iPatientId" >
                        <itemtemplate>
                        <asp:Label ID="lbliPatientId" runat ="server" Text='<%#Eval("iPatientId") %>' >
                        </itemtemplate>
                        
                        
                        <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") %>' />
                        <table>
                        <tr>
                        <td colspan="6">
                        <asp:GridView ID="grvCPTICD" runat ="server" AutoGenerateColumns="false" HeaderStyle-BackColor="#3399FF" HeaderStyle-ForeColor="White">
                                              
                                             
                        
            </columns>
                <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)
    {
        try
        {
            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 = "";
                    grvSub.DataBind();
                    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;
        BindGridview();
         }
 
can any one send the solution plz
 
Thanks in advance
 
Murthy
Posted 2-Jul-12 0:21am
Edited 2-Jul-12 1:14am
v3
Comments
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
good
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"
CommandArgument="<%#((GridViewRow)Container).RowIndex%>"/>
 

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;
  Permalink  
v2
Comments
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 8,284
1 Sergey Alexandrovich Kryukov 7,407
2 DamithSL 5,614
3 Maciej Los 4,989
4 Manas Bhardwaj 4,986


Advertise | Privacy | Mobile
Web03 | 2.8.1411023.1 | Last Updated 2 Jul 2012
Copyright © CodeProject, 1999-2014
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