gridview 中的删除命令问题。
目前想做一个类似 用datagrid的例子,
于是照着一个datagrid的代码改。
在.aspx中添加了
<asp:ButtonField Text= "delete " HeaderText= "delete " CommandName= "Delete " ButtonType= "Button "> </asp:ButtonField>
在.aspx.cs中添加了
protected void Gridview_Delete(object sender, GridViewCommandEventArgs e)
{//use WEBCONFIG connection
string conn = ConfigurationSettings.AppSettings[ "strconn "];
SqlConnection st = new SqlConnection(conn);
String delsql = "delete from stock where stid=@stid ";
SqlCommand mycommand = new SqlCommand(delsql,st);
mycommand.Parameters.Add(new SqlParameter( "@stid ",SqlDbType.Int));
mycommand.Parameters[ "@stid "].Value=GridView1.DataKeys[(int)e.Item.ItemIndex];
st.Open();
try{
mycommand.ExecuteNonQuery();
Response.Write( " <script language=javascript> alert( 'delete succeed ') </script> ");
}
catch(SqlException)
{
Response.Write( " <script language=javascript> alert( 'delete failed ') </script> ");
}
st.Close();
BindGrid();
}
可是调试报: 'System.Web.UI.WebControls.GridViewCommandEventArgs ' does not contain a definition for 'Item '
出错语句是:mycommand.Parameters[ "@stid "].Value=GridView1.DataKeys[(int)e.Item.ItemIndex];
看书上的例子也没有找到定义Item的地方
请问,在使用gridview中,这个Item是如何定义的呢?
或者有其他解决办法来解决在gridview中添加一个按钮实现删除的功能呢。。
------解决方案--------------------
写gridview的OnRowDeleting事件啊
<asp:GridView ID= "GridView1 " runat= "server " OnRowDeleting= "Gridview_Delete "> </asp:GridView>