dave_gr
asked on
DataGridView - Stop row delete after user prompt 'Cancel' clicked
Hi All,
Apologies if this is a duplicate post - I don't see my earlier question listed.
I prompt the user to confirm they want to delete a row in a DataGridView with the code below.
How do I roll back the delete both in the DataGridView _and_ the DataTable. I want to avoid refreshing the DataGridView as this takes them back to the top.
Thanks,
David
void users_sel_RowDeleting(obje ct sender, DataRowChangeEventArgs e) {
// Check if the user wants to delete the selected user
string dialogMessage = "Are you SURE you want to delete this user?";
string dialogCaption = "Delete User?";
MessageBoxButtons buttons = MessageBoxButtons.OKCancel ;
DialogResult result = MessageBox.Show(dialogMess age, dialogCaption, buttons, MessageBoxIcon.Question, MessageBoxDefaultButton.Bu tton2);
if (result != DialogResult.OK) {
// User cancelled deletion..... How do I keep the row in the data table as well as the data grid view???
// My attempts at doing this:
e.Row.EndEdit(); // ???
e.Row.CancelEdit(); // This seems to work on the DataTable but the datagridview still gets removed. I want to keep that row aswell
e.Row.RejectChanges(); // ???
this.users_selBindingSourc e.CancelEd it(); // ???
}
}
Apologies if this is a duplicate post - I don't see my earlier question listed.
I prompt the user to confirm they want to delete a row in a DataGridView with the code below.
How do I roll back the delete both in the DataGridView _and_ the DataTable. I want to avoid refreshing the DataGridView as this takes them back to the top.
Thanks,
David
void users_sel_RowDeleting(obje
// Check if the user wants to delete the selected user
string dialogMessage = "Are you SURE you want to delete this user?";
string dialogCaption = "Delete User?";
MessageBoxButtons buttons = MessageBoxButtons.OKCancel
DialogResult result = MessageBox.Show(dialogMess
if (result != DialogResult.OK) {
// User cancelled deletion..... How do I keep the row in the data table as well as the data grid view???
// My attempts at doing this:
e.Row.EndEdit(); // ???
e.Row.CancelEdit(); // This seems to work on the DataTable but the datagridview still gets removed. I want to keep that row aswell
e.Row.RejectChanges(); // ???
this.users_selBindingSourc
}
}
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
if (MessageBox.Show("Are you SURE you want to delete this user?", "Delete User?", MessageBoxButtons.OKCancel
e.Cancel = true;