?
Solved

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

Posted on 2006-11-16
2
Medium Priority
?
718 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

[Webinar] How Hackers Steal Your Credentials

Do You Know How Hackers Steal Your Credentials? Join us and Skyport Systems to learn how hackers steal your credentials and why Active Directory must be secure to stop them.

Question has a verified solution.

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

Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
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…
NetCrunch network monitor is a highly extensive platform for network monitoring and alert generation. In this video you'll see a live demo of NetCrunch with most notable features explained in a walk-through manner. You'll also get to know the philos…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…
Suggested Courses

764 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