2016-03-07 91 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 
相关问题