Click here to Skip to main content
15,888,018 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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.NET
<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

C#
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

C#
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
Updated 2-Jul-12 0:14am
v3
Comments
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

Try updating you image button html somelike below

XML
<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

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


New Code

C#
ImageButton imgBtn = (ImageButton)sender;
 
Share this answer
 
v2
Comments
Murthy_RDV 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_RDV 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)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900