I'm trying to build a winform with a Delete button and DataGridView. What I want is to click a row in DatagridView and then click Delete button. The selected row will be removed from DataGridView and database too. Here's my uncompleted code:
private void btnDelete_Click(object sender, EventArgs e)
{
if (dgvKH.SelectedRows.Count > 0)
{
int selectedIndex = dgvKH.SelectedRows[0].Index;
}
Load();
}
it depends on your datagridview. if it has some sort of primary key you can do that by update Query. here is some code
private void btnDelete_Click(object sender, EventArgs e)
{
if (dgvKH.SelectedRows.Count > 0)
{
int selectedIndex = dgvKH.SelectedRows[0].Index;
int PrimaryKey =Convert.ToInt32(dgvKH.Rows[selectedIndex].Cells["ID"].Value);
/*
* Execute this Query HERE.
string sql = "DELETE From MyTable WHERE ID = " + PrimaryKey;
*/
dgvKH.Rows.RemoveAt(selectedIndex);
}
Load();
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.