【问题标题】:Change page on GridView if edit row this return error如果编辑行返回错误,则更改 GridView 上的页面
【发布时间】:2019-10-08 00:45:57
【问题描述】:

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

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

我正在尝试将数据逐行添加到 datagridview 这是我的代码,它说:

"索引超出范围。必须为非负数且小于大小 集合参数名称: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>

【问题讨论】:

  • 您实际上向您的方法发送了什么输入?你能举一个它崩溃的例子吗?现在看来,rowindex 要么从单击第一页上的上一个返回 -1,要么因为您的目标行还不存在而太大?
  • rowindex 变量中的值是多少?您单击 GridView 中的哪个按钮来执行gvProducts_RowCommand 代码?
  • @ChetanRanpariya rowindex 的值总是正确的,因为我在gvProducts_RowCommand 中添加了Response.Write(rowindex); Response.End();。 GridView 中用于执行的按钮请参阅我的第一个问题中的 Edit #01。谢谢。
  • rowindex 值正确时不会出现异常。所以你需要观察它的价值,在这里分享一下。
  • @ChetanRanpariya 我对GridView g2 = (GridView)gvProducts.Rows[rowindex].FindControl("GridView2"); 有例外,在rowindex 没有例外

标签: c# gridview pagination


【解决方案1】:

你需要替换这个:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-08-01
    相关资源
    最近更新 更多