【问题标题】:System.ArgumentOutOfRangeException occurs while updating the datatable from the gridview从 gridview 更新数据表时发生 System.ArgumentOutOfRangeException
【发布时间】:2015-01-16 13:39:16
【问题描述】:

我正在使用 Rowcommand 事件更新网格视图中的行以及 DataTable,但是在将值分配给 DataTable 中的行时发生异常。

 protected void grduser_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName == "Update")
        {
            DataTable dt = (DataTable)ViewState["dtable"];

            Int32 index = Convert.ToInt32(e.CommandArgument);

            GridViewRow row = grduser.Rows[index];
           // after this statement the exception occurs!
            dt.Rows[row.DataItemIndex]["userid"] = ((TextBox)(row.Cells[0].Controls[0])).Text;
            dt.Rows[row.DataItemIndex]["username"] = ((TextBox)(row.Cells[1].FindControl("txtuname"))).Text;
            dt.Rows[row.DataItemIndex]["usertype"] = ((TextBox)(row.Cells[2].Controls[0])).Text;
            dt.Rows[row.DataItemIndex]["email"] = ((TextBox)(row.Cells[3].Controls[0])).Text;
            dt.Rows[row.DataItemIndex]["salary"] = ((TextBox)(row.Cells[4].FindControl("txtsalary"))).Text;
            grduser.EditIndex = -1; 
            ViewState["dtable"]=dt;
            grduser.DataSource = (DataTable)(ViewState["dtable"]);
            grduser.DataBind();

        }

    }

//下面的网格视图源码。

       <asp:GridView ID="grduser" runat="server" AutoGenerateColumns="False" 
        onrowediting="grduser_RowEditing"   onrowupdating="grduser_updateRow"
         >

<Columns>
<asp:BoundField   HeaderText="Id" DataField="userid"/>
    <asp:TemplateField HeaderText="Name">
        <EditItemTemplate>
            <asp:TextBox ID="txtuname" runat="server" Text='<%# Bind("username") %>'></asp:TextBox>
        </EditItemTemplate>
        <ItemTemplate>
            <asp:Label ID="Label1" runat="server" Text='<%# Bind("username") %>'></asp:Label>
        </ItemTemplate>
    </asp:TemplateField>
 <asp:BoundField HeaderText="User Type" DataField="usertype" />
 <asp:BoundField HeaderText="Email" DataField="email" />
    <asp:TemplateField HeaderText="Salary">
        <EditItemTemplate>
            <asp:TextBox ID="txtsalary" runat="server" Text='<%# Bind("salary") %>'></asp:TextBox>
        </EditItemTemplate>
        <ItemTemplate>
            <asp:Label ID="Label2" runat="server" Text='<%# Bind("salary") %>'></asp:Label>
        </ItemTemplate>
    </asp:TemplateField>

【问题讨论】:

  • 注释行正下方的行
  • 可能在这里GridViewRow row = grduser.Rows[index]
  • 不,它发生在下一行
  • 检查这个((TextBox)(row.Cells[0].Controls[0])).Text 是否提供了一些东西? row.DataItemIndex这也是,它给了什么?
  • @yogi 是的,这句话有问题:**粗体((TextBox)(row.Cells[0].Controls[0])).Text**,它抛出异常。当我早些时候在 Row_updating 中检查它时它正在工作,但现在当我在 Row_command 中使用它时

标签: c# asp.net exception gridview datatable


【解决方案1】:

您收到该错误是因为您的 e.CommandArgument 不是您的 GridView 中的有效行号。例如,您的 grduser 有 3 行,而您的 e.CommandArgument 设置为 3。这将抛出 System.ArgumentOutOfRangeException,因为 grduser.Rows 是从 0 开始的,您很可能从 1 开始设置它。

【讨论】:

  • 实际上并不是通过选择设置它,我是在点击更新时直接获取该特定行的索引。
  • @psylogic 我知道您在点击时获取它。我是说它以前在初始绑定上设置不正确,当您使用 e.CommandArgument 时。
  • 那么我们该如何解决这个问题呢?因为据我所知,该行正在触发事件,因此索引应该属于该行。请在这个问题上启发我,因为我非常愿意了解更多,尽管我确实以不同的方式解决了这个问题。
  • @psylogic 缺少太多信息,无法准确阐述。您已经解决了您的问题,但是通过更改代码而不是解决问题。无论哪种方式都有效。 :)
【解决方案2】:

你可以试试这个。

protected void grduser_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName == "Update")
        {
            DataTable dt = (DataTable)ViewState["dtable"];

            Int32 index = Convert.ToInt32(e.CommandArgument);

            GridViewRow row = grduser.Rows[index];

            dt.Rows[row.DataItemIndex]["userid"] = row.Cells[0].Text;
            dt.Rows[row.DataItemIndex]["username"] = ((TextBox)(row.FindControl("txtuname"))).Text;
            dt.Rows[row.DataItemIndex]["usertype"] = row.Cells[2].Text;
            dt.Rows[row.DataItemIndex]["email"] = row.Cells[3].Text;
            dt.Rows[row.DataItemIndex]["salary"] = ((TextBox)(row.FindControl("txtsalary"))).Text;
            grduser.EditIndex = -1; 
            ViewState["dtable"]=dt;
            grduser.DataSource = (DataTable)(ViewState["dtable"]);
            grduser.DataBind();

        }

    }

【讨论】:

  • NOPE NOT WORK FOR THE BOUND FIELDS!
  • 你试过调试吗?我已经在我的机器上尝试了几次,它可以工作。你确定它不工作吗?
  • @Chris OP 得到 System.ArgumentOutOfRangeException - 它在问题标题中。
  • 他已经将我的答案标记为已回答,然后将其收回。
  • 我明白了。我没有意识到这一点。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-12-08
  • 1970-01-01
  • 2013-11-12
  • 1970-01-01
  • 2022-10-24
  • 1970-01-01
相关资源
最近更新 更多