如果编辑行出现此 return 错误,则在 GridView 上更改页面

Change page on GridView if edit row this return error

我知道Whosebug 中存在这个问题。但我的似乎不同。我看不出有什么问题。但它有时会在运行时在 gridview 上更改页面时发生。

如果在 Gridview 的第一页上工作,我在 gridview 的编辑行上没有错误。 如果更改 Gridview 页面并尝试编辑任何行 return 错误。

我正在尝试将数据一行一行地添加到数据网格视图中,这是我的代码,上面写着:

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

这是什么意思?我的代码有什么问题吗?

线路错误:

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

这是我的代码:谁能看到并告诉我发生了什么?

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>

编辑#01

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

你需要替换这个:

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

有:

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