[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

DataGridView - Stop row delete after user prompt 'Cancel' clicked

Posted on 2006-11-16
2
Medium Priority
?
721 Views
Last Modified: 2011-10-03
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(); // ???
    }            
}
0
Comment
Question by:dave_gr
  • 2
2 Comments
 
LVL 6

Expert Comment

by:so3
ID: 17958965
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;
     


 
0
 
LVL 6

Accepted Solution

by:
so3 earned 2000 total points
ID: 17958981
this should look the code in the UserDeletingRow event

private void dataGridView1_UserDeletingRow(object sender, DataGridViewRowCancelEventArgs e)
        {
            if (MessageBox.Show("Are you SURE you want to delete this user?", "Delete User?", MessageBoxButtons.OKCancel, MessageBoxIcon.Question) != DialogResult.OK)
                e.Cancel = true;
        }
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
Is your data getting by on basic protection measures? In today’s climate of debilitating malware and ransomware—like WannaCry—that may not be enough. You need to establish more than basics, like a recovery plan that protects both data and endpoints.…
Whether it be Exchange Server Crash Issues, Dirty Shutdown Errors or Failed to mount error, Stellar Phoenix Mailbox Exchange Recovery has always got your back. With the help of its easy to understand user interface and 3 simple steps recovery proced…
Suggested Courses
Course of the Month19 days, 16 hours left to enroll

872 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question