DataGrid本身就有DeleteCommand事件。

解决方案 »

  1.   

    我知道但是不知道怎么用,能给个示例代码么,aspx和cs的都要。
      

  2.   

    cs的在ItemDataBound事件里
    SqlConnection myConn = new SqlConnection(ConfigurationSettings.AppSettings["connStr"]);
    myConn.Open() ;
    string SQL="delete from tblName where iId="+id;
    SqlCommand myCommand = new SqlCommand(SQL,myConn);
    myCommand.ExecuteNonQuery();
    myConn.Close() ;
      

  3.   

    datagrid添加删除列,设置DataKeyField="主键字段"
    在DeleteCommand事件中
    e是 DataGridCommandEventArgs e
    string id = datagrid.DataKeys[e.Item.ItemIndex].ToString();
    string aSql = "delete from table where id=" + id;
    //调用Common对象的方法
    comm.ExecuteNonQuery()
      

  4.   

    Apollo2003(Apollo2003) 说的很详细了。
      

  5.   

    http://chs.gotdotnet.com/quickstart/aspplus/doc/webdataaccess.aspx#delete
      

  6.   

    asp:
    <asp:DataGrid id="DataGrid1" runat="server" DataKeyField="字段"></asp:DataGrid>
    cs:
    private void DataGrid1_DeleteCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
    {
    string id = DataGrid1.DataKeys[e.Item.ItemIndex].ToString();
    //delete方法
    }
      

  7.   

    <asp:DataGrid id="DataGrid1" runat="server" DataKeyField="字段">
    <Columns>
    <asp:ButtonColumn Text="delete" CommandName="delete"></asp:ButtonColumn>
    </Columns>
    </asp:DataGrid>
      

  8.   

    我试了还是不行,他就不执行delete的那个事件,我在里边放了个label他根本就显示不出来
      

  9.   

    <ASP:DataGrid id="MyDataGrid" runat="server"
          DataKeyField="id"
          OnDeleteCommand="MyDataGrid_Delete"
        >
    public void MyDataGrid_Delete(Object sender, DataGridCommandEventArgs e)
        {
            String deleteCmd = "DELETE from Employee where id = @Id";        SqlCommand myCommand = new SqlCommand(deleteCmd, myConnection);
            myCommand.Parameters.Add(new SqlParameter("@Id", SqlDbType.NVarChar, 11));
            myCommand.Parameters["@Id"].Value = MyDataGrid.DataKeys[(int)e.Item.ItemIndex];        myCommand.Connection.Open();        try
            {
                myCommand.ExecuteNonQuery();
                Message.InnerHtml = "<b>已删除记录</b><br>" + deleteCmd;
            }
            catch (SqlException)
            {
                Message.InnerHtml = "错误:未能删除记录";
                Message.Style["颜色"] = "红色";
            }        myCommand.Connection.Close();        BindGrid();
        }
      

  10.   

    另外检查一下事件注册在不在
    private void InitializeComponent()
    {    
    this.MyDataGrid.DeleteCommand += new System.Web.UI.WebControls.DataGridCommandEventHandler(this.MyDataGrid_Delete);
    }
      

  11.   

    FROM 子句语法错误。 
    说明: 执行当前 Web 请求期间,出现未处理的异常。请检查堆栈跟踪信息,以了解有关该错误以及代码中导致错误的出处的详细信息。 异常详细信息: System.Data.OleDb.OleDbException: FROM 子句语法错误。源错误: 
    行 63:  string aSql = "delete from table where ID="+id;
    行 64:  OleDbCommand comm=new OleDbCommand(aSql,mycon);
    行 65:             comm.ExecuteNonQuery();
    行 66:  grid.DataBind();
    行 67:  mycon.Close();
     
      

  12.   

    FROM 子句语法错误.看看id值是多少?字段里有没有ID字段?表名对了没有~?
      

  13.   

    我的cs
    public void DeleteCommand(object sender, DataGridCommandEventArgs e)
    {
    string constr;
    constr="Provider=Microsoft.Jet.OLEDB.4.0;Data Source="+Server.MapPath("db/db1.mdb");
    OleDbConnection mycon=new OleDbConnection(constr);
    mycon.Open();
    string id = grid.DataKeys[e.Item.ItemIndex].ToString();
    string aSql = "delete from table where ID="+id;
    OleDbCommand comm=new OleDbCommand(aSql,mycon);
                comm.ExecuteNonQuery();
    grid.DataBind();
    mycon.Close();
    prompt.Text="888";
    }
      

  14.   

    aspx
    <asp:datagrid id="grid" DataKeyField="ID" runat="server" OnDeleteCommand="DeleteCommand" OnPageIndexChanged="changegrid" AllowPaging="True" Width="600px" CellPadding="3" BorderWidth="1px" BorderColor="Black" PageSize="5" AutoGenerateColumns="False" HeaderStyle-BackColor="#aaaadd" Font-Size="10pt" HorizontalAlign="Left" Font-Names="宋体">