目前想做一个类似 用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中添加一个按钮实现删除的功能呢。。

解决方案 »

  1.   

    DataGrid1.DataKeys.Item[e.Item.ItemIndex]
      

  2.   

    VB:    Sub DataGrid_Delete(ByVal sender As System.Object, ByVal e As DataGridCommandEventArgs)
            Dim conn As New OleDbConnection
            conn.ConnectionString = StrCon
            conn.Open()
            Dim sid As Int16 = myDataGrid.DataKeys.Item(e.Item.ItemIndex)
            strsql = "delete from akind where id=" & sid & ""
            Dim strcomm As New OleDbCommand(strsql, conn)
            strcomm.ExecuteNonQuery()
            conn.Close()
            Label3.Text = "成功删除分类"
            Label3.ForeColor = Drawing.Color.Red
            strsql = "SELECT id,kind1,kind2,mcon from akind where uname='" & Session("acountuser") & "'"
            BindList()
        End Sub
      

  3.   

    GridView1.DataKeys[e.Row.RowIndex]。。如上
      

  4.   

    Gridview啊,看错了gridview批量删除:
        Sub button2_click(ByVal s As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
            Dim rowindex As Int16
            Dim suc As Boolean = False
            GridView1.EditIndex = -1
            Dim conn As New OleDbConnection
            conn.ConnectionString = StrCon
            conn.Open()
            Dim strcomm As New OleDbCommand
            strcomm.Connection = conn
            For rowindex = 0 To GridView1.Rows.Count - 1
                If CType(GridView1.Rows(rowindex).FindControl("chkSelect"), CheckBox).Checked = True Then
                    strsql = "delete from shouzhi where id=" & GridView1.DataKeys.Item(rowindex).Value & ""
                    strcomm.CommandText = strsql
                    strcomm.ExecuteNonQuery()
                    suc = True
                End If
            Next
            conn.Close()
            strsql = ViewState("strsql")
            BindList()
            If suc = True Then
                Label3.Text = "删除成功"
                Label3.ForeColor = Drawing.Color.Red
            Else
                Label3.Text = "未选择任何项目"
                Label3.ForeColor = Drawing.Color.Red
            End If
        End Sub
      

  5.   

    报错:'System.Web.UI.WebControls.GridViewCommandEventArgs' does not contain a definition for 'Row'
    现在是row没有定义。。另外有点基础疑问请教,在.aspx中的CommandName="Delete" 是如何与
    .aspx.cs中的protected void Gridview_Delete 关联起来呢,程序怎么知道我点击button是执行了 Gridview_Delete 这个函数呢?
      

  6.   

    另外我的程序是在vwd2005中用c#写的。
      

  7.   

    写gridview的OnRowDeleting事件啊<asp:GridView ID="GridView1" runat="server" OnRowDeleting="Gridview_Delete"></asp:GridView>