private void DataGrid1_ItemCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
if(e.CommandName=="删除")
  {
  string strSQL="delete  FROM yonghuxinxi where ID="+ e.Item.Cells[2].Text.Trim();
//你的删除代码
  }
}

解决方案 »

  1.   

    假设id是第二列
    String strSQL="delete  FROM yonghuxinxi where ID="+e.Item.Cells[1].Text.Trim()
      

  2.   

    String strSQL="delete  FROM yonghuxinxi where ID='"+DataGrid1.SelectedItem.Cells[2].Text+"'";
      

  3.   

    把你的DataGrid的DataKeyField设为数据库字段id,然后上面的语句这以这样写:
    String strSQL="delete  FROM yonghuxinxi where ID="+(int)DataGrid1.DataKeys[e.Item.ItemIndex];
      

  4.   

    这样是不行的
    应先删除后,再用Select * from yonghuxinxi
    然后绑定,删除语句同timmy310 
    String strSQL="delete  FROM yonghuxinxi where ID='"+DataGrid1.SelectedItem.Cells[2].Text+"'";
      

  5.   

    如果id是数字:
    String strSQL="delete  FROM yonghuxinxi where ID="+e.Item.Cells[2].Text;   
    是字符串:
    String strSQL="delete  FROM yonghuxinxi where ID='"+e.Item.Cells[2].Text+"'";
      

  6.   

    to:2002pine(我学习,我存在) 
    我试了,这样的确有问题,请问你说的"应先删除后,再用Select * from yonghuxinxi"
    要用到2个String strSQL吗?应该怎样写啊?
    我这样写
    SqlConnection cn=new SqlConnection("data source=NET-001;initial catalog=aspceshi;user id=sa;password=admini");
    String strSQL="delete  FROM yonghuxinxi where ID=="+e.Item.Cells[3].Text.Trim();
    String strSQL1="SELECT ID, PASSWORD, SEX, YONGHUMING FROM yonghuxinxi";
    SqlDataAdapter objAdapter= new SqlDataAdapter(strSQL,cn);
                SqlDataAdapter objAdapter1= new SqlDataAdapter(strSQL1,cn);
    DataSet objDataSet= new DataSet();
    objAdapter1.Fill(objDataSet); cn.Close();
    DataGrid1.DataSource=objDataSet;
                    
    DataGrid1.DataBind();
    好象没有删除!
      

  7.   

    private void DataGrid1_DeleteCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
    {
    SqlConnection cn=new SqlConnection("data source=数据库名;initial catalog=aspceshi;user id=sa;password=admini");
    String strSQL="delete  FROM yonghuxinxi where ID='"+e.Item.Cells[2].Text+"'";
      
    SqlCommand cmd = new SqlCommand( strSQL,cn );
             cn.Open();
             cmd.ExecuteNonQuery();
    cn.Close();
    }
      

  8.   

    e.Item.Cells[i].Text  取得单元格中数据
      

  9.   

    to:timmy3310(Tim)
    我试了一下你的方法说这里
    cmd.ExecuteNonQuery();
    有语法错误。