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

C# 从GridView删除行时索引超出范围异常,c#,asp.net,gridview,C#,Asp.net,Gridview,我犯了这样的错误 “索引超出范围。必须为非负且小于大小。” “藏品目录” 尝试删除gridview的行时。我的aspx.cs代码如下: protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e) { try { main delete=new main(); // int id = Convert.ToInt32(GridView1.Rows[e.R

我犯了这样的错误

“索引超出范围。必须为非负且小于大小。” “藏品目录”

尝试删除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);
    }
}

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

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

int rowIndex = int.Parse(e.CommandArgument.ToString());
string val = (string)this.grid.DataKeys[rowIndex]["myKey"];
在按钮上使用:

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

我建议使用参数来避免

可能是
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);
    }
}
Convert.ToInt32(GridView1.DataKeys[e.RowIndex][0].ToString()); // 0 is the index and in this case Id will be at zero
或者像这样:

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);
    }
}
Convert.ToInt32(GridView1.DataKeys[e.RowIndex][0].ToString()); // 0 is the index and in this case Id will be at zero

其中,
nameOfKey
是表的列名,在您的情况下,它是带有_puja的
GridView 1
必须是
e.Item
,如果
e
具有
Item
属性。

GridView
中的
数据键类似于:

 DataKeyNames="Id, foo, foo"
然后尝试使用如下字符串:

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);
    }
}
Convert.ToInt32(GridView1.DataKeys[e.RowIndex][0].ToString()); // 0 is the index and in this case Id will be at zero
试试这个

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

sum+=Convert.ToInt32(GridView1.Rows[i]。单元格[3]。文本)

好吧,这个问题让我很恼火,但直到今天我才明白

我的情况:

  • 数据库新手

  • 建立我自己的数据库

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

  • 出于某种原因,我的桌子上只有一张有这个问题


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

这就解决了这个问题

但这只是真正问题的一个线索。在我的例子中,令人不快的表格阅读量要大得多,只是偶尔会改变,所以MyISAM就是我想要的

这里的关键区别是MyISAM对输入更严格。正如您在上面看到的,有很多Int32转换正在进行。那么,试试这个:

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

希望这能为其他人节省一些时间和悲伤。

检查e.RowIndex的值,它一定是错误的。您确实需要先对代码格式和命名进行排序。我遇到了一个错误,因为Gridview不包含项的定义。就像我说的,将
GridView1
替换为
e.Item
ah尝试从网格的
DataSource
中提取它,就像我在更新的回答中包含的内容一样。我得到的错误列(带_puja)在表中不存在,但我在表中有。应该在[“”]中给出哪一列?谢谢nathan的回答,但我认为现在我的deletedata函数不正确。查询结果如何?你只需用val替换你的答案,我更新了答案。你可以通过解释这到底有什么帮助来改进你的答案。