有谁知道怎么删除dataGrid的数据吗?语言是C#,是winform,不是在webform中呀,
本人等啊

解决方案 »

  1.   

    我是想在界面中直接删除,就是说直接选中datagrid中的这个行,然后执行删除按钮操作啊,
      

  2.   

    从数据源中删除也要通过代码标识该datagrid列中的或者行啊,需要建变量啊,问题是找不到datagrid中什么样方法能将值赋给cmd.parameters[].value啊,
      

  3.   

    private void  Delbutton_Click(object sender,  System.EventArgs e)
    {
    int i = dataGrid.CurrentRowIndex;
    if (i==-1){MessageBox.Show("请先在表中选择需要删除的数据行!","错误",MessageBoxButtons.OK,MessageBoxIcon.Exclamation);}
    else
    {
    try
    {
    sqlConnection1.Open();
    SqlCommandBuilder sqlCommandBuilder1 = new SqlCommandBuilder ( sqlDataAdapter1);
    ds.Tables["Cable_info"].Rows[i].Delete();
    sqlDataAdapter1.Update(ds,"Cable_info");
    ds.Tables["Cable_info"].AcceptChanges();
    sqlConnection1.Close();
    MessageBox.Show("已经成功删除数据","删除成功",MessageBoxButtons.OK,MessageBoxIcon.Information);

    catch (System.Exception e1) {MessageBox.Show("" ,"错误",MessageBoxButtons.OK,MessageBoxIcon.Error);}
                      
    } }
    这段代码删除dataGrid中当前选中行,并且更新所关联的数据库(不用更新数据库的可以去掉),ds是绑定在dataGrid上的DataSet
      

  4.   

    int rowi;
    private void dataGrid1_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
    {
    System.Drawing.Point  pt= new   Point(e.X,e.Y);       
    DataGrid.HitTestInfo  hti= this.dataGrid1.HitTest(pt);       
    if(hti.Type==DataGrid.HitTestType.Cell)       
    {     
    //this.dataGrid1.CurrentCell   =   new   DataGridCell(hti.Row,   hti.Column);
    rowi=hti.Row;
    this.dataGrid1.Select(hti.Row);
    }     
    }private void button1_Click(object sender, System.EventArgs e)
    {
    ds.Tables[0].Rows[rowi].Delete();
    //已经得到了行标,你可以在这里执行sql删除数据库里的数据了
    this.dataGrid1.DataSource=ds.Tables[0].DefaultView;
    }
      

  5.   

    SqlConnection con = new Sqlcon("server = .;database = (你的数据库);user = (你的用户名);password = (你的密码)";
    con.Open();
    SqlCommand cmd = new SqlCommand("delete (你的表) where (表的id)= @id",con);
    pt = new SqlParameter("@id",SqlDbType.VarChar,6);
    pt.Value = e.Item.Cells[0].Text;
    cmd.Parameters.Add(pt);cmd.ExecuteNonQuery();DataGrid1.EditItemIndex = -1;
    DataGrid1.DataBind();         //DataGrid1绑定
      

  6.   

    在womform中根本主不支持e.Item.Cells[0].Text这种方法啊,是不是我没有using什么呢?请指教!!
    楼上的这位同志,那里不是表的id吧,是表里某个字段的id吧,呵呵,
    我就是找不到这种方法呀,