2013-11-25 95 views
0

我如何使用C#中的datagridview中的MYSQL刪除數據SELECTED(CHECKED)?C#DatagridVIew刪除SQL日期

我的代碼:

string CONFIG = "server=localhost;userid=root;password=admin;database=program"; 
MySqlConnection cone = new MySqlConnection(CONFIG); 

MySqlCommand query2 = new MySqlCommand(); 
query2.Connection = cone; 
query2.CommandText = "DELETE FROM TABLE WHERE ID = @ID"; 
// I believe the key from this problem stay in line below// 
query2.Parameters.AddWithValue("@ID", gridMovie.SelectedRows); 

    cone.Open(); 
    query2.ExecuteNonQuery(); 
    cone.Close(); 

回答

3

在GridView的SelectedRow屬性賦予整個行。所以你需要提供列值(在你的情況下它是ID) -

query2.Parameters.AddWithValue("@ID", gridMovie.SelectedRow 
       .Cells[indexofIDcolumn].Value)