I have a simple DataGridView on a WinForm application where I am using the UserDeletingRow event on the gridview. When I highlight the row I want to delete, it of course prompts me if I want to truly delete the row, but if I answer "NO", the row disappears from the grid. It's not deleted off the database, but it vanishes off the grid. If I get out of this form, and go back in, the row is back again! Why does it disappear in the first place?
The code of course says: if(result == DialogResult.Yes) then I execute my DELETE statement. That part works just fine for getting rid of the row off the database. But I'm answering NO!
private void DataGridView1_UserDeletingRow(object sender, DataGridViewRowCancelEventArgs e)
{
if (dataGridView1.CurrentRow.Cells["ID"].Value != DBNull.Value)
{
DialogResult result = MessageBox.Show("Are you sure you want to delete speaker -- " + dataGridView1.CurrentRow.Cells["FIRSTNAME"].Value + " " + dataGridView1.CurrentRow.Cells["LASTNAME"].Value + "?", "Warning", MessageBoxButtons.YesNo, MessageBoxIcon.Warning);
if (result == DialogResult.Yes)
{
using (SQLiteCommand cmd = Business.Business.GetSqliteCommand())
{
string sql = "DELETE FROM Speakers WHERE ID = " + "'" + dataGridView1.CurrentRow.Cells["ID"].Value + "'";
cmd.CommandText = sql;
cmd.ExecuteNonQuery();
Main_Reload(null, null);
}
}
}
I'm expecting the grid to not do anything if I choose NO!
Add an else statement to your IF block
else
{
e.Cancel = true;
Main_Reload(null, null);
}
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.