注册 登录
编程论坛 ASP.NET技术论坛

GridView删除行错误

pietro 发布于 2006-11-17 22:10, 2676 次点击
GridView删除行代码如下:

string userID = this.GridView1.DataKeys[e.RowIndex].Values[0].ToString();
SqlConnection con = new SqlConnection("server=.;database=login;uid=sa;pwd=;");
con.Open();
SqlCommand cmd = new SqlCommand("delete from login where userID='" + userID + "'", con);
cmd.ExecuteNonQuery();
this.GridViewToBind();

错误提示:“System.Web.UI.WebControls.GridViewDeletedEventArgs”并不包含“RowIndex”的定义
请问那里错了,该怎么写才对???
7 回复
#2
刘小芳2006-11-18 02:57

主键设置在属性里

#3
pietro2006-11-18 08:24
能不能说详细一点,我是新手
#4
hoya2006-11-18 08:40
GridView1.DataKeys[e.RowIndex].Values[0].ToString(); >==

GridView1.DataKeys[e.item.itemindex]

试试
#5
tel19822007-05-12 13:13
你把主键绑定到你的gridview上了吗?
#6
luyuan03162007-08-29 20:26

兄弟我给你个正解:
你的这段代码可能是写错地方啦,在GridView控件的“删除”列的事件中有RowDeleted和RowDeleting,前者是“对数据库执行Delete命令后激发”,后者是前激发。你把这段代码写在RowDeleted下就会出现这个错误提示:“System.Web.UI.WebControls.GridViewDeletedEventArgs”并不包含“RowIndex”的定义。
那是你的错误,应该写在RowDeleting事件下就没问题了。

#7
师妃暄2007-08-30 14:21
楼上正解
#8
梦灵儿2007-08-31 18:41
呵呵,楼上楼上的支持你,不过在下还要补充一句。千万别忘了在gridview中的属性:DataKeyNames这个属性里写要按哪个字段删除的就要写一哈不然计算机会不知道是按什么删除的。会找不到主键的。呵呵
1