完全从MS Access数据库和Gridview Winforms Devexpress中删除?

时间:2014-02-08 13:06:53

标签: c# winforms gridview devexpress

我可以删除gridview临时中的选定行,当我再次打开同一个表单时,它会再次使用此代码显示所有行

gridview1.DeleteselectedRows(); 

但是我还需要从数据库中完全删除,所以我尝试了这段代码,但是这段代码删除了gridview中所选行的前一行或前一行。这段代码出了什么问题?

gridview1.DeleteselectedRows();                                                         object temp = gridView1.GetFocusedRowCellValue("InvoiceNumber");

validateDept.InitializeConnection();
OleDbConnection con = new OleDbConnection(validateDept.connetionString);
con.Open();
OleDbCommand delete1 = new OleDbCommand("delete from NewInvoice_1 where InvoiceNumber=" + temp, con);
OleDbCommand delete2 = new OleDbCommand("delete from NewInvoice_2 where InvoiceNumber=" + temp, con);
delete1 .ExecuteNonQuery();
delete2 .ExecuteNonQuery();
con.Close();

1 个答案:

答案 0 :(得分:0)

validateDept.InitializeConnection();
OleDbConnection con = new OleDbConnection(validateDept.connetionString);
con.Open();
OleDbCommand delete1 = new OleDbCommand("delete from NewInvoice_1 where InvoiceNumber=" +        temp, con); 
OleDbCommand delete2 = new OleDbCommand("delete from NewInvoice_2 where InvoiceNumber=" + temp, con);
delete1 .ExecuteNonQuery();
delete2 .ExecuteNonQuery();
con.Close();

首先不要先写gridview1.DeleteselectedRows();这行,因为它会从gridview&中删除所选行。跳转到下一行然后数据库将删除下一行,因此出现问题。