[英]Trying to delete selected row from datagridview but it is deleting multiple rows
It is a simple question, but since I am new to C#, I am not sure how to solve this. 这是一个简单的问题,但由于我是C#的新手,我不知道如何解决这个问题。 Basically, I have a datagridview that displays records from MySQL table. 基本上,我有一个显示MySQL表记录的datagridview。 I have a delete button, which by clicking performs the sql query, which is working fine, and is also meant to then delete the selected row from datgridview. 我有一个删除按钮,通过单击执行sql查询,这是正常工作,并且还意味着从datgridview删除选定的行。 But what actually happening, is it deletes multiple rows even when only one row is selected. 但实际发生的是,即使只选择了一行,它也会删除多行。 Here is the query: 这是查询:
private void delete_btn_Click(object sender, EventArgs e)
{
foreach (DataGridViewRow row in dataGridView1.Rows)
{
string constring = @"server = localhost; user id = root; password = pass; persistsecurityinfo = false; database = mapping; allowuservariables = false";
using (MySqlConnection con = new MySqlConnection(constring))
{
using (MySqlCommand cmd = new MySqlCommand("UPDATE deletion SET date_time = UTC_TIMESTAMP() where product_id =" + proid_txtbx.Text, con))
{
cmd.Parameters.AddWithValue("@product_id", row.Cells["product_id"].Value);
cmd.Parameters.AddWithValue("@product_name", row.Cells["product_name"].Value);
cmd.Parameters.AddWithValue("@category_id", row.Cells["category_id"].Value);
cmd.Parameters.AddWithValue("@date_time", row.Cells["date_time"].Value);
con.Open();
cmd.ExecuteNonQuery();
}
foreach(DataGridViewRow item in this.dataGridView1.SelectedRows)
{
dataGridView1.Rows.RemoveAt(this.dataGridView1.SelectedRows[0].Index);
}
}
}
Screenshots: Row 6 should be deleted: 截图: 应删除第6行: Other Rows are deleted when I click Delete button 单击“删除”按钮时,将删除其他行
I think your problem is with your foreach
loop. 我认为你的问题在于你的foreach
循环。 You are looping through all your rows with dataGridView1.Rows
. 您正在使用dataGridView1.Rows
循环遍历所有行。 Tr dataGridView1.SelectedRows
instead: 改为:tr dataGridView1.SelectedRows
:
foreach (DataGridViewRow row in dataGridView1.SelectedRows)
if (!row.IsNewRow) dataGridView1.Rows.Remove(row);
How about creating new command, somethnig like... 怎么样创建新命令,有点像...
DELETE FROM YourTable WHERE product_id = ?
After that you can get value of selected item index: 之后,您可以获得所选项目索引的值:
int product_id = Convert.ToInt32(DataGridView1.SelectedRows[0].Cells[0].Value)
And at the end run the command and pass it the product_id int you got from command. 最后运行命令并将命令传递给你的product_id int。 I think it should work without problems... 我认为它应该没有问题...
You can use: 您可以使用:
private void delete_btn_Click(object sender, EventArgs e)
{
//Works even when whole row is selected.
int rowIndex = datagridview.CurrentCell.RowIndex;
//You can also then easily get column names / values on that selected row
string product_id = datagridview.Rows[rowIndex].Cells["Column Name Here"].Value.ToString();
//Do additional logic
//Remove from datagridview.
datagridview.Rows.RemoveAt(rowIndex);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.