Solved

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

Posted on 2006-11-16
2
705 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 500 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

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

Suggested Solutions

Summary: Persistence is the capability of an application to store the state of objects and recover it when necessary. This article compares the two common types of serialization in aspects of data access, readability, and runtime cost. A ready-to…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

707 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now