【问题标题】:Index out of range exception while deleting row from GridView从 GridView 删除行时索引超出范围异常
【发布时间】:2014-02-18 07:42:24
【问题描述】:

我收到类似

的错误

"索引超出范围。必须为非负数且小于大小 合集”

尝试删除 gridview 行时。我的 aspx.cs 代码如下:

protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
    try
    {
        main delete=new main();

        // int id = Convert.ToInt32(GridView1.Rows[e.RowIndex].Cells["id"].Value);
        // string ID = GridView1.Rows[e.RowIndex].ToString();
        sQLcONN.Open();
        string Query = "delete  * from shoppingcart where shoppingcart.with_puja = '"+Convert.ToInt32(GridView1.DataKeys[e.RowIndex].Value.ToString())+"' ";
        delete.deleteData(Query);
        Bindgrid();
        sQLcONN.Close();
    }
    catch (Exception ex)
    {
        Console.WriteLine("{0} Exception caught.", ex);
    }
}

【问题讨论】:

  • 检查e.RowIndex的值,一定是错的
  • 您确实需要先对代码格式和命名进行排序。

标签: c# asp.net gridview


【解决方案1】:

也许DataKeys部分是罪魁祸首,它一定是这样的:

string Query = "delete from shoppingcart where shoppingcart.with_puja = '" + Convert.ToInt32(e.Item.DataKeys["nameOfKey"].Value).ToString() + "'";

或类似的东西:

protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
    try
    {
        main delete = new main();
        DataTable dt = GridView1.DataSource as DataTatable;

        sQLcONN.Open();
        string Query = "delete from shoppingcart where shoppingcart.with_puja = '" + Convert.ToInt32(dt.Rows[e.RowIndex]["nameOfKey"]).ToString() + "'";
        delete.deleteData(Query);
        Bindgrid();
        sQLcONN.Close();

    }
    catch (Exception ex)
    {
        Console.WriteLine("{0} Exception caught.", ex);
    }
}

其中nameOfKey 是表的列名,在您的情况下为with_puja,如果e 具有Item 属性,则GridView1 必须为e.Item。

【讨论】:

  • 我收到一个错误,因为 Gridview 不包含 Item 的定义。
  • 如我所说,将GridView1替换为e.Item
  • 啊尝试从网格的DataSource 中提取它,就像我在更新的答案中包含的那样
  • 我收到错误列 (with_puja) 未在表中退出,但我在表中。应该在 [""] 中给出哪一列?
  • 感谢 nathan 的回答,但我认为现在我的 deletedata 函数不正确。
【解决方案2】:

GridView 中的 DataKeys 将类似于:

 DataKeyNames="Id, foo, foo"

然后尝试使用这样的字符串:

Convert.ToInt32(GridView1.DataKeys[e.RowIndex][0].ToString()); // 0 is the index and in this case Id will be at zero

【讨论】:

    【解决方案3】:

    好吧,这个问题把我惹恼了 Bejebus,但直到今天才弄清楚。

    我的情况:

    • 数据库新手。

    • 设置我自己的数据库。

    • 将 ASP.NET 与 MySQL、phpMyAdmin 一起使用

    • 由于某种原因,我只有一张桌子出现了这个问题。


    在寻找解决方案时,我决定打开我的数据库并将有问题的表的存储引擎从 MyISAM 更改为 InnoDB。

    仅此一项就解决了问题。

    但这只是真正问题的线索。在我的情况下,有问题的表要读取更多,只是偶尔更改,所以 MyISAM 是我想要的。

    关键区别是 MyISAM 对输入的要求更严格。正如您在上面看到的,正在进行大量的 Int32 转换。所以,试试这个:

    • 打开你的数据库,表
    • 检查表的 id 字段
    • 它的类型是某种形式的“Int”
    • 值/长度字段是否不是 32?

    希望能为别人节省一些时间和悲伤。

    【讨论】:

      【解决方案4】:

      试试这个

      string Query = "delete * from shoppingcart where shoppingcart.with_puja = '" + Convert.ToInt32(GridView1.DataKeys[e.RowIndex].Values["Id"].ToString()) + "'";
      

      【讨论】:

        【解决方案5】:

        您可以将行索引作为 CommandArgument 传递。

        在服务器上的事件方法中:

        int rowIndex = int.Parse(e.CommandArgument.ToString());
        string val = (string)this.grid.DataKeys[rowIndex]["myKey"];
        

        关于按钮使用:

        CommandArgument='<%# DataBinder.Eval(Container, "RowIndex") %>'
        

        编辑:

        查询:

        string Query = "delete  * from shoppingcart where shoppingcart.with_puja = '"+val +"' ";
        

        我建议使用参数来避免sql injections。

        【讨论】:

        • 查询巢穴如何?
        • 你只需用 val 替换你所拥有的,我更新了答案
        【解决方案6】:

        sum += Convert.ToInt32(GridView1.Rows[i].Cells[3].Text);

        【讨论】:

        • 您可以通过解释这究竟有什么帮助来改进您的答案。
        猜你喜欢
        • 1970-01-01
        • 2011-03-16
        • 1970-01-01
        • 1970-01-01
        • 2013-02-26
        • 2011-12-25
        相关资源
        最近更新 更多