2016-03-07 93 views
1

我試圖創建將包含綁定到一個數據源的數據一個DataGridView的應用程序。我在Form_Load上使用fill方法,我想知道如何刪除一個或多個複選框行,不僅從我的表中,而且還從數據庫/數據源同時刪除。C#中刪除從DataGridView行和更新數據庫

我用的刪除按鈕的代碼,但它不會永久刪除所選行。任何幫助?

for (int i = 0; i < Products.Rows.Count; i++) 
{ 
    DataGridViewRow dr = Products.Rows[i]; 
    if (dr.Selected == true) 
    { 
     Products.Rows.RemoveAt(i); 
    } 
} 
+0

您將需要一個數據庫調用刪除所選行。 –

+0

數據庫已經存在,並在應用程序運行時加載。問題是,當通過「刪除」按鈕刪除1個或多個複選框時,這些行會消失,但如果我重新啓動程序,它們會再次回來。任何想法如何更新數據庫上的這些更改? – SarahF

+0

你能指定你正在使用的數據庫嗎? MS SQL Server或MySQL或Oracle等? –

回答

0

您必須將必要的調用寫入您的數據庫管理器。如果你使用MySQL,那麼你可以使用:

for (int i = 0; i < Products.Rows.Count; i++) 
{ 
    DataGridViewRow dr = Products.Rows[i]; 
    if (dr.Selected == true) 
    { 
     Products.Rows.RemoveAt(i); 
     // CODE ADDED 
     MySqlCommand cmd = sql.CreateCommand(); // creates the MySQL object needed for queries (I think there's another way also, but I use this) 
     cmd.CommandText = "DELETE FROM table WHERE id = 1"; // sets the query 

     try 
     { 
      cmd.ExecuteNonQuery(); // executes the query 
     } 
     catch(MySqlException e) 
     { 
      sql.Close(); 
      Console.WriteLine(e.Message); 
     } 
     sql.Close(); 
     // CODE ADDED 
    } 
} 

sql是爲MySqlConnection sql之前定義的,它的初始化是:

try { 
    sql = new MySqlConnection({SQL_CONNECTION_STRING}); 
    try 
    { 
     sql.Open(); 
     sql.Close(); // Close the DB. This block is useful to check whether or not the connection was successfully opened 
    } 
    catch (MySqlException e) 
    { 
     Console.WriteLine(e.Message); 
    } 
} 
catch (MySqlException e) 
{ 
    Console.Write(e.Message); 
} 
0
if (MessageBox.Show("Confirm ?","DELETE", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes) 
{ 
    string name = dataGridView1.Rows[e.RowIndex].Cells[WHEREINDEX].Value.ToString(); 
    sql = "DELETE FROM [TABLE] WHERE [id] = " + dataGridView1.Rows[e.RowIndex].Cells[WHEREINDEX].Value.ToString(); 

    if (db.Exec(sql) > 0) 
    { 
     MessageBox.Show(name + " deleted"); 
     dtSummary.Rows.Find(dataGridView1.Rows[e.RowIndex].Cells[1].Value.ToString()).Delete(); 
     dataGridView1.Refresh(); 
    } 
    else 
    { 
     MessageBox.Show("error while deleting") 
    } 
} 
0

你在做什麼被刪除所選行僅限於DataGridView

你沒有做任何數據庫調用刪除行。

我假設你正在使用Microsoft SQL Server。

在您需要獲得一個唯一標識產品的一些情況。例如產品ID。

假設您已將數據庫中的ProductId列綁定到DataGridView中的某個列。

您的代碼應該如下所示。

//string variable to capture product Ids for selected products 
System.Text.StringBuilder productIds = new System.Text.StringBuilder(string.empty); 

for (int i = 0; i < Products.Rows.Count; i++) 
{ 
    DataGridViewRow dr = Products.Rows[i]; 

    if (dr.Selected == true) 
    { 
     Products.Rows.RemoveAt(i); 

     productIds.Append(productIds.length > 0 ? "," + Convert.ToString(dr["ProductId"]) : Convert.ToString(dr["ProductId"])); 
    } 
} 

DeleteProducts(productIds.ToString()); 

現在你的DeleteProducts方法應該如下。

private int DeleteProducts(string productIds) 
{ 
    int recordsDeleted = 0; 

    using (SqlConnection conn = new SqlConnection("Your connection string here")) 
    { 
     try 
     { 
      using (SqlCommand cmd = new SqlCommand("Your SQL Stored Procedure name here", conn)) 
      { 
       cmd.CommandType = CommandType.StoredProcedure; 

       SqlParameter paramProductIds = new SqlParameter("@productIds", varchar(2000)); 
       paramProductIds.Value = productIds; 

       cmd.Parameters.Add(paramProductIds); 

       conn.Open(); 

       recordsDeleted = cmd.ExecuteNonQuery(); 
      } 
     } 
     finally { conn.Close(); } 

    } 

    return recordsDeleted; 
} 

而你的存儲過程應該如下(假設MS SQL Server)。

CREATE PROCEDURE DeleteProducts 
    @productIds VARCHAR(2000) 
AS 
BEGIN 
    DELETE FROM Products WHERE ProductId IN (SELECT item FROM dbo.Split(',', @productIds)) 
END 
相關問題