2014-02-08 80 views
0

我可以能夠刪除所選行中的GridView暫時的,當我再次打開相同的形式再次使用此代碼從MS Access數據庫和Gridview Winforms完全刪除Devexpress?

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(); 
+0

您確定,您的臨時對象是具有正確值InvoiceNumber的字符串嗎? – potehin143

+0

是的,它獲得了正確的價值,但現在更新了代碼i中的小修正 – Srihari

+1

您的gridview1.DeleteselectedRows()是先完成然後刪除命令?如果是的話,它會刪除你想要聚焦的那一行,並將焦點移到下一行 – potehin143

回答

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 &跳轉到下一行則數據庫會刪除下一行選定的行,所以會出現問題。