Link to home
Start Free TrialLog in
Avatar of dave_gr
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(object 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(dialogMessage, dialogCaption, buttons, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2);

    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_selBindingSource.CancelEdit(); // ???
    }            
}
Avatar of so3
so3
Flag of Romania image

if you are using datagridview you can use the event UserDeletingRow to cancel by using e.Cancel=true

if (MessageBox.Show("Are you SURE you want to delete this user?", "Delete User?", MessageBoxButtons.OKCancel, MessageBoxIcon.Question) != DialogResult.OK)
          e.Cancel = true;
     


 
ASKER CERTIFIED SOLUTION
Avatar of so3
so3
Flag of Romania image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial