C#控件中的DATAGRID中的删除按钮怎么写代码?

解决方案 »

  1.   

    是web哦,在DataGrid 的属性生成器里面添加了个删除列,不晓得怎么写代码。
      

  2.   

    private void DataGrid1_DeleteCommand(object source, DataGridCommandEventArgs e)
    {
    SqlConnection cn = new SqlConnection( connectionString);
    string  strDelete = "delete from Note Where id = @id";
    SqlCommand cmd = new SqlCommand(strDelete, cn );
    cmd.Parameters.Add("@id", SqlDbType.VarChar).Value = DataGridView.DataKeys[e.Item.ItemIndex].ToString();
    cn.Open();
    cmd.ExecuteNonQuery();
    cn.Close();
    DataGrid1.EditItemIndex = -1;
    BindDataGrid();//重新绑定到数据库
    }
      

  3.   

    DataGridView 改成DataGrid1 DataGrid必须设置DataKeyField属性,最好是表的主键