0

我是 Asp.net 的新手。单击网格上的链接按钮后,我希望删除网格视图行。我正在尝试使用 datakeys 获取我单击的 gridview 行的主键,并将其传递给 Delete 存储过程。但是我得到了 index out of range 的异常。请帮助我我该怎么做?这是我的代码

GridView grid=(GridView)sender;
LinkButton lnkbtndelete = (LinkButton)e.CommandSource;
GridViewRow grdvwrw = (GridViewRow)lnkbtndelete.NamingContainer;
int pkey = Convert.ToInt32(grid.DataKeys[grdvwrw.RowIndex].Value.ToString());
SqlCommand cmd3 = new SqlCommand("USP_DELETE_NOTICE", con2);
cmd3.CommandType = CommandType.StoredProcedure;
cmd3.Parameters.AddWithValue("@NOTICEID", pkey);
int rowsdeleted = cmd3.ExecuteNonQuery();`enter code here`
4

0 回答 0