读书人

怎么在删除dataGridView中的记录并且

发布时间: 2012-09-05 15:19:34 作者: rapoo

如何在删除dataGridView中的记录,并且是真正的执行数据库删除?
如题,有一个删除的button,在dataGridView1里面,点button删除了记录,但是查询的话,依然还在,并没有从数据库中删除

百度找了一个代码,好像没用

C# code
       int r = 0;        string c = "";        DataSet ds = new DataSet();        private void button1_Click(object sender, EventArgs e)        {            DataSet ds = new DataSet();            if (skey.Text.Trim() == "")            {                MessageBox.Show("请输入分类名称", "提示");                return;            }            string strConnect = Properties.Settings.Default.PopInfoConnectionString;            SqlConnection conConnection = new SqlConnection(strConnect);            conConnection.Open();            string cmd = " select classname as 产品分类名称,uptime as 最后更新日期 from [zmlb] where classname='" + skey.Text.Trim() + "'";            SqlDataAdapter ada = new SqlDataAdapter(cmd, conConnection);            ada.Fill(ds);            dataGridView1.DataSource = ds.Tables[0];        }        private void dataGridView1_RowEnter(object sender, DataGridViewCellEventArgs e)        {            r = e.RowIndex; /*获得具有焦点的行*/            c = dataGridView1.Rows[r].Cells[1].Value.ToString();        }        private void button2_Click(object sender, EventArgs e)        {            dataGridView1.Rows.RemoveAt(r);        }        private void dataGridView1_RowsRemoved(object sender, DataGridViewRowsRemovedEventArgs e)        {            string strConnect = Properties.Settings.Default.PopInfoConnectionString;            SqlDataAdapter ada = new SqlDataAdapter();            SqlConnection conConnection = new SqlConnection(strConnect);            SqlCommand com = new SqlCommand("Delete From zmlb Where classname=@x", conConnection);            com.Parameters.Add("@x", SqlDbType.Char, 10);            com.Parameters[0].Value = c; /*对数据库进行操作的参数添加*/            ada.DeleteCommand = com;            ada.Update(ds);        }


[解决办法]
C# code
public int UpdateByDataSet(DataSet ds,string strTblName,string strConnection){   try  {   SqlConnection  conn = new SqlConnection(strConnection));   SqlDataAdapter myAdapter = new SqlDataAdapter();   SqlCommand myCommand = new SqlCommand("select * from "+strTblName),(SqlConnection)this.conn);      myAdapter.SelectCommand = myCommand;   SqlCommandBuilder myCommandBuilder = new SqlCommandBuilder(myAdapter);       myAdapter.Update(ds,strTblName);    return 0;  }  catch(BusinessException errBU)  {    throw errBU;  }    catch(Exception err)  {    throw new BusinessException(err);  }}private void button2_Click(object sender, EventArgs e){  dataGridView1.Rows.RemoveAt(r);  UpdateByDataSet(dataGridView1.DataSource as DataTable,"zmlb",Properties.Settings.Default.PopInfoConnectionString);}
[解决办法]
C# code
private void dataGridView1_RowsRemoved(object sender, DataGridViewRowsRemovedEventArgs e)        {//...com.ExecuteNonQuery();//加一句执行删除的就可以了ada.Update(ds);}
[解决办法]
请不要高估你的描述能力或者我的理解能力。
[解决办法]
if (DataGridView.IsCurrentCellDirty) DataGridView.CommitEdit(DataGridViewDataErrorContexts.Commit);
xx.Update(xx)

读书人网 >C#

热点推荐