指数超出范围的异常,而从GridView中删除一行 [英] Index out of range exception while deleting row from GridView
本文介绍了指数超出范围的异常,而从GridView中删除一行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我收到错误
索引超出范围。必须大于大小非负和少
的
集
块引用>试图删除的GridView的行时。我aspx.cs code是如下:
保护无效GridView1_RowDeleting(对象发件人,GridViewDeleteEventArgs E)
{
尝试
{
主删除=新的main(); // INT ID = Convert.ToInt32(GridView1.Rows [e.RowIndex] .Cells [身份证]值。);
//字符串ID = GridView1.Rows [e.RowIndex]的ToString();
sQLcONN.Open();
字符串查询='+ Convert.ToInt32(GridView1.DataKeys [e.RowIndex] .Value.ToString())+从哪里购物= shoppingcart.with_puja删除*';
delete.deleteData(查询);
Bindgrid();
sQLcONN.Close();
}
赶上(异常前)
{
Console.WriteLine({0}捕获到异常。恩);
}
}
解决方案也许
DataKeys
部分是罪魁祸首,它必须是这样的:字符串查询=从购物车中删除,其中shoppingcart.with_puja ='+ Convert.ToInt32(e.Item.DataKeys [nameOfKey]。值)的ToString()+ ';
或者是这样的:
保护无效GridView1_RowDeleting(对象发件人,GridViewDeleteEventArgs E)
{
尝试
{
主删除=新的main();
DataTable的DT = GridView1.DataSource为DataTatable; sQLcONN.Open();
字符串查询=从购物车中删除,其中shoppingcart.with_puja ='+ Convert.ToInt32(dt.Rows [e.RowIndex] [nameOfKey])的ToString()+';
delete.deleteData(查询);
Bindgrid();
sQLcONN.Close(); }
赶上(异常前)
{
Console.WriteLine({0}捕获到异常。恩);
}
}其中,
nameOfKey
是是with_puja
在您的情况和GridView1
必须e.Item
如果电子
有一个项目
属性。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. My aspx.cs code is below:
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: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 iswith_puja
in your case andGridView1
must bee.Item
ife
has anItem
property.这篇关于指数超出范围的异常,而从GridView中删除一行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文