根据文本框值从datagridview中删除行

时间:2015-12-14 13:24:42

标签: c# datagridview row mysql-workbench

我在表单中有一个文本框,显示所选行的ID。我想在删除按钮上添加一个查询,该查询根据文本框值从datagridview中删除行。 enter image description here

到目前为止,我的删除按钮代码是:

 private void delete_Click(object sender, EventArgs e)
    {
        try
        {
            MySqlConnection con = new MySqlConnection("server = localhost; user id = root; password = pass; persistsecurityinfo = false; database = mapping; allowuservariables = false");
            con.Open();
            MySqlCommand commandDelete = new MySqlCommand("DELETE from products WHERE product_id = @proid_txtbx", con);
            commandDelete.Parameters.AddWithValue("@proid_txtbx", product_ID);
            commandDelete.ExecuteNonQuery();
        }
        catch (MySqlException ex)
        {
            MessageBox.Show(ex.ToString());
        }
    }

它无法正常工作。

2 个答案:

答案 0 :(得分:1)

如果product_ID是您的文本框的名称,那么您最后会遗漏.Text:

commandDelete.Parameters.AddWithValue("@proid_txtbx", product_ID.Text);

答案 1 :(得分:0)

我解决了。这是由于这条线:

MySqlCommand commandDelete = new MySqlCommand("DELETE from products WHERE product_id=" + @proid_txtbx.Text, con);

谢谢大家的帮助。