【问题标题】:Update or Delete in Gridbox C#在 Gridbox C# 中更新或删除
【发布时间】:2016-05-24 10:11:20
【问题描述】:

我已经编写了用于更新、删除、取消和编辑 GridBox 中的行的代码。 取消和编辑工作正常。但是当我点击更新或删除时,我收到一条错误提示

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

对于更新,代码是

protected void Show_Grid_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
    int id = int.Parse(Show_Grid.DataKeys[e.RowIndex].Value.ToString());
    TextBox txt_title = (TextBox)Show_Grid.Rows[e.RowIndex].FindControl("Title");
    TextBox txt_desc = (TextBox)Show_Grid.Rows[e.RowIndex].FindControl("Description");
    TextBox drop_prior = (TextBox)Show_Grid.Rows[e.RowIndex].FindControl("Priority");

    Update_todo(id, txt_title.Text, txt_desc.Text, drop_prior.Text);
    Show_Grid.EditIndex = -1;
    DataBind();
}

数据库包含,

id, Title, Description, Priority

【问题讨论】:

  • 为什么要分配“Show_Grid.EditIndex = -1;”?
  • @brokenisfixed 根据创建的 ID 索引数据库中的值。
  • Show_Grid.EditIndex = -1;应该只是表明该行不再被编辑...

标签: c# .net gridview


【解决方案1】:

我们可以使用 GridView 的定义,但这里有几件事需要检查/尝试:

您是否指定了数据键名称?

DataKeyNames="id"

您是否尝试在第一行代码上设置断点并逐步找出是哪一行代码引发了错误?

【讨论】:

  • 是的,我已经检查过了。 int id = int.Parse(Show_Grid.DataKeys[e.RowIndex].Value.ToString()); 抛出错误
  • Show_Grid.DataKeys[e.RowIndex].Value 的值是多少?
猜你喜欢
  • 2017-08-16
  • 2018-02-06
  • 1970-01-01
  • 2018-11-20
  • 2017-09-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多