当前位置:编程学习 > C#/ASP.NET >>

怎么删除DataGridView中的一行数据

点击button的时候,怎么删除DataGridView中的一行数据
  
            
 int index = 0;//dataGridView1中ID列的索引
            string temp = string.Format("delete goods where ID={0}",
            dataGridView1[index, dataGridView1.SelectedCells[0].RowIndex].Value);
            dataGridView1.Rows.RemoveAt(dataGridView1.SelectedCells[0].RowIndex);

这样只能在显示时删除表面的,但是数据库里还是没删除该行数据啊 --------------------编程问答-------------------- 它们不是联动的,删除row的时候,直接sql语句删除 delete * from xxxx --------------------编程问答-------------------- 你只是删了查询出来的一行显示数据,而真正影响到数据库的只有下SQL delete才行.
--------------------编程问答-------------------- 可以直接选中一行,然后del就行了。。不过,如果想在后台数据库也删掉数据的话,必须得加上代码。
SqlCommandBuilder scb = new SqlCommandBuilder(sda);
            sda.Update(ds, "course"); --------------------编程问答-------------------- 嗯 我就是想同时删除数据库的行数据

   private void button4_Click(object sender, EventArgs e)
        {
            mycon = new SqlConnection(constr);
            int index = 0;//dataGridView1中ID列的索引
            string temp = string.Format("delete goods where ID={0}",
            dataGridView1[index, dataGridView1.SelectedCells[0].RowIndex].Value);
            dataGridView1.Rows.RemoveAt(dataGridView1.SelectedCells[0].RowIndex);
            SqlDataAdapter da = new SqlDataAdapter("delete goods where id={0}", mycon);
            DataSet ds = new DataSet();
            da.Fill(ds);
            SqlCommandBuilder scb = new SqlCommandBuilder(da);
              da.Update(ds, "course");
            

          
        }


但是还是有异常 “0”附近有错误!   --------------------编程问答-------------------- 就如一楼所说的 --------------------编程问答-------------------- 我想请问LZ的 temp 变量 在哪里执行?
你只是写了一个delete语句,却没有提交执行 --------------------编程问答-------------------- 但是我不知道怎么把我选中datagridview中的一行数据与数据库中对应的那行联系起来?  --------------------编程问答-------------------- 用bingdingSource关联Datagridview和DataTable,用bingdingSource的RemoveAt删除。 --------------------编程问答-------------------- --------------------编程问答-------------------- 你真幸运,我也刚刚解决了这个问题。


            //---------------消除最后一行---------------
            dataGridView1.AllowUserToAddRows = false;
补充:.NET技术 ,  C#
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,