简体   繁体   English

从GridView中删除行时索引超出范围异常

[英]Index out of range exception while deleting row from GridView

I am getting error like 我得到的错误就像

"Index was out of range. Must be non-negative and less than the size of the collection" “指数超出范围。必须是非负数且小于集合的大小”

when trying to delete row of gridview. 当试图删除gridview行时。 My aspx.cs code is below: 我的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);
    }
}

Maybe the DataKeys part is the culprit, it must be something like this: 也许DataKeys部分是罪魁祸首,它必须是这样的:

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

or something like this: 或类似的东西:

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);
    }
}

Where nameOfKey is the column name of the table which is with_puja in your case and GridView1 must be e.Item if e has an Item property. nameOfKey是作为表的列名with_puja你的情况和GridView1必须e.Item如果eItem属性。

Your DataKeys in GridView would be like: GridView DataKeys就像:

 DataKeyNames="Id, foo, foo"

and then try to use string like this: 然后尝试使用这样的字符串:

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

Alright, this question annoyed the Bejebus out of me, but couldn't figure it out until today. 好吧,这个问题让Bejebus感到厌烦,但直到今天才弄明白。

My Situation: 我的情况:

  • A database novice. 一个数据库新手。

  • Set up my own database. 设置我自己的数据库。

  • Using ASP.NET with MySQL, phpMyAdmin 使用ASP.NET与MySQL,phpMyAdmin

  • For some reason, only one of my table's had this issue. 出于某种原因,我的桌子中只有一个有这个问题。


In searching for a solution, I decided to open up my database and change the offending table's storage engine from MyISAM to InnoDB . 在搜索解决方案时,我决定打开我的数据库并将有问题的表的存储引擎从MyISAM更改为InnoDB

And that alone fixed the issue. 仅这一点就解决了这个问题。

But that's only a clue as to the real problem. 但这只是真正问题的线索 In my case, the offending table was far more read-heavy, changing only occasionally, so MyISAM is what I wanted it to be. 就我而言,有问题的表格更加重读,只是偶尔改变,所以MyISAM就是我想要的。

The key difference here is that MyISAM is stricter with input. 这里的关键区别是MyISAM对输入更严格。 As you can see above, there's a lot of Int32 converting going on. 正如您在上面所看到的,有很多Int32转换正在进行中。 So, try this: 所以,试试这个:

  • Open up your database, table 打开你的数据库,表
  • Check your table's id field 检查表格的id字段
  • Is its type some form of "Int" 它的类型是某种形式的“Int”
  • Is the Value/Length field anything other than 32? Value / Length字段是否为32以外的值?

Hope that saves someone else some time and grief. 希望能为他人节省一些时间和悲伤。

You can pass the row index as CommandArgument. 您可以将行索引作为CommandArgument传递。

In the event method on server: 在服务器上的事件方法中:

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

On the button use: 在按钮上使用:

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

EDIT: 编辑:

Query: 查询:

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

I recommend to use parameters to avoid sql injections . 我建议使用参数来避免sql注入

尝试这个

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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM