Solved

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

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

Industry Leaders: 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

Suggested Solutions

Title # Comments Views Activity
How come this regular expression allows "e"? 14 59
Sort GridView by ID Descending 1 24
asp web application 3 42
Convert VB web project to C# 3 39
Introduction This article series is supposed to shed some light on the use of IDisposable and objects that inherit from it. In essence, a more apt title for this article would be: using (IDisposable) {}. I’m just not sure how many people would ge…
This article describes a simple method to resize a control at runtime.  It includes ready-to-use source code and a complete sample demonstration application.  We'll also talk about C# Extension Methods. Introduction In one of my applications…
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

733 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