locked
Change page on GridView if edit row this return error RRS feed

  • Question

  • User1151703306 posted

    Hello there, I need your help.

    If working on first page of Gridview I don't have error on edit row in gridview. If change page of Gridview and try for editing any row return error.

    I'm trying to add data as one by one row to a datagridview here is my code and it says:

    "Index was out of range. Must be non-negative and less than the size of the collection parameter name:index"

    What does this means ? Is there any problem in my code.

    Line error :

    GridView g2 = (GridView)gvProducts.Rows[rowindex].FindControl("GridView2");

    Here is my code : Could anyone please see and tell me what happens?

    Please can you help me ?

    protected void gvProducts_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName != "Page")
        {
            int rowindex = Convert.ToInt32(e.CommandArgument.ToString());
            GridView g2 = (GridView)gvProducts.Rows[rowindex].FindControl("GridView2");
    
            if (e.CommandName == "Details")
            {
                int customerId = (int)this.gvProducts.DataKeys[rowindex]["sID"];
    
                gvProducts.Rows[rowindex].FindControl("btn_Show").Visible = false;
    
                sql = @String.Format(" SELECT * FROM `doTable` ");
                sql += String.Format(" WHERE ");
                sql += String.Format(" sID IN ('{0}') ", customerId);
    
                g2.DataSource = GetData(sql);
                g2.DataBind();
                g2.Visible = true;
            }
            else
            {
                g2.Visible = false;
                gvProducts.Rows[rowindex].FindControl("btn_Show").Visible = true;
    
            }
        }
    }
    
    
    protected void Paginate(object sender, CommandEventArgs e)
    {
        int intCurIndex = gvProducts.PageIndex;
    
        switch (e.CommandArgument.ToString().ToLower())
        {
            case "First":
                gvProducts.PageIndex = 0;
                break;
            case "Prev":
                gvProducts.PageIndex = intCurIndex - 1;
                break;
            case "Next":
                gvProducts.PageIndex = intCurIndex + 1;
                break;
            case "Last":
                gvProducts.PageIndex = gvProducts.PageCount - 1;
                break;
        }
        gvProducts.DataBind();
    }
    
                <PagerTemplate>
                    <asp:ImageButton ID="ImageButton1" runat="server" ImageUrl="/aspnet/img/bot_back.gif"
                        CommandArgument="First" CommandName="Page" />
                    <asp:ImageButton ID="ImageButton2" runat="server" ImageUrl="/aspnet/img/bot_back2.gif"
                        CommandArgument="Prev" CommandName="Page" />
                    Page
                        <asp:DropDownList ID="ddlPages" runat="server" AutoPostBack="True" CssClass="ddl_Class"
                            OnSelectedIndexChanged="DDLPages_SelectedIndexChanged">
                        </asp:DropDownList>
                    of
                        <asp:Label ID="lblPageCount" runat="server"></asp:Label>
                    <asp:ImageButton ID="ImageButton3" runat="server" ImageUrl="/aspnet/img/bot_next.gif"
                        CommandArgument="Next" CommandName="Page" />
                    <asp:ImageButton ID="ImageButton4" runat="server" ImageUrl="/aspnet/img/bot_next2.gif"
                        CommandArgument="Last" CommandName="Page" />
                </PagerTemplate>
    
    <asp:TemplateField HeaderText="" ItemStyle-HorizontalAlign="Center">
       <ItemTemplate>
          <asp:ImageButton ID="btn_Show" runat="server"
           CommandName="Details"
           CommandArgument='<%#Container.DataItemIndex%>' />
       </ItemTemplate>
    </asp:TemplateField>



    Monday, October 7, 2019 1:48 PM

Answers

  • User314352500 posted

    You need replace this :

    <asp:TemplateField HeaderText="" ItemStyle-HorizontalAlign="Center">
       <ItemTemplate>
          <asp:ImageButton ID="btn_Show" runat="server"
           CommandName="Details"
           CommandArgument='<%#Container.DataItemIndex%>' />
       </ItemTemplate>
    </asp:TemplateField>

    With :

    <asp:TemplateField HeaderText="" ItemStyle-HorizontalAlign="Center">
       <ItemTemplate>
          <asp:ImageButton ID="btn_Show" runat="server"
           CommandName="Details"
           CommandArgument='<%# DataBinder.Eval(Container, "RowIndex") %>'/>
       </ItemTemplate>
    </asp:TemplateField>

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, October 7, 2019 4:47 PM

All replies

  • User314352500 posted

    You need replace this :

    <asp:TemplateField HeaderText="" ItemStyle-HorizontalAlign="Center">
       <ItemTemplate>
          <asp:ImageButton ID="btn_Show" runat="server"
           CommandName="Details"
           CommandArgument='<%#Container.DataItemIndex%>' />
       </ItemTemplate>
    </asp:TemplateField>

    With :

    <asp:TemplateField HeaderText="" ItemStyle-HorizontalAlign="Center">
       <ItemTemplate>
          <asp:ImageButton ID="btn_Show" runat="server"
           CommandName="Details"
           CommandArgument='<%# DataBinder.Eval(Container, "RowIndex") %>'/>
       </ItemTemplate>
    </asp:TemplateField>

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, October 7, 2019 4:47 PM
  • User-1716253493 posted

    Try this

    if (e.CommandName != "Page")
        {
            if (e.CommandName == "Details")
            {
                int rowindex = Convert.ToInt32(e.CommandArgument.ToString());
                GridView g2 = (GridView)gvProducts.Rows[rowindex].FindControl("GridView2");
                int customerId = (int)this.gvProducts.DataKeys[rowindex]["sID"];
    
                gvProducts.Rows[rowindex].FindControl("btn_Show").Visible = false;
    
                sql = @String.Format(" SELECT * FROM `doTable` ");
                sql += String.Format(" WHERE ");
                sql += String.Format(" sID IN ('{0}') ", customerId);
    
                g2.DataSource = GetData(sql);
                g2.DataBind();
                g2.Visible = true;
            }

    Tuesday, October 8, 2019 8:56 AM
  • User288213138 posted

    Hi Golia,

    "Index was out of range. Must be non-negative and less than the size of the collection parameter name:index"

    According to your description and code, i can't reproduce your question.

    My suggestion on this error is to set breakpoints to debug your code.

    If you still can't solve the problem,please post aspx and aspx.cs code. I will help you to find out where the problem is.

    Best regards,

    Sam

    Tuesday, October 8, 2019 10:06 AM