Solved

Revert changes back in Rowvalidating event of datagridview

Posted on 2010-08-20
5
1,221 Views
Last Modified: 2013-11-27
Hi Experts,
I  am using visual studio 2005 and working on Win forms application. I have a datagridview. I prompt the user to save if the data has changed in rowValidating event.
If the user says "yes", everything is fine. But if users says "no", then I manage not to save the data., but I visually also want to remove the changes. how do i revert back the changes? Attached is my code. Any help will be greatly appreciated

Private Sub DataGridView1_RowValidated(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.RowValidated
        Dim lResponse As Long
        If UpdatePending Then
            lResponse = MessageBox.Show("Do you wish to save changes?", "Data has Changed", MessageBoxButtons.YesNo)
            If lResponse = vbYes Then
                Try
                    Dim command_builder As SqlCommandBuilder
                    command_builder = New SqlCommandBuilder(daHotel)
                    Me.daHotel.Update(dsHotel.Tables(0))
                    DataGridView1.Refresh()
                    Me.UpdatePending = False
                Catch ex As Exception
                    MessageBox.Show(ex.Message)
                End Try
            Else
                DataGridView1.Refresh()
                Me.UpdatePending = False
            End If
        Else
            Me.UpdatePending = False
        End If
    End Sub

Open in new window

0
Comment
Question by:RekhaShah
  • 3
5 Comments
 
LVL 9

Expert Comment

by:puru1981
ID: 33490696
for this you need to handle CancelRowEdit event.

visit the link below

http://msdn.microsoft.com/en-us/library/15a31akc.aspx
0
 
LVL 18

Expert Comment

by:John (Yiannis) Toutountzoglou
ID: 33490721
hi..
Just use Me.DatagridView.CancelEdit in your Else Condition
0
 
LVL 18

Accepted Solution

by:
John (Yiannis) Toutountzoglou earned 500 total points
ID: 33490753
also When you are using a dataset ..you can Set .....MyDataset.RejectChanges
0
 
LVL 18

Expert Comment

by:John (Yiannis) Toutountzoglou
ID: 33490790
try
Dim dt As DataTable = TryCast(Me.dataGridView1.DataSource, DataTable)
dt.RejectChanges()
0
 

Author Closing Comment

by:RekhaShah
ID: 33497880
Thank  you. It worked.
0

Featured Post

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
C# Offline Apllication 5 71
VB.Net Tranfer data between to VB.Net apps 8 25
Help with preventing downloading a zip file 10 37
SQL Query Help Top 1 and Distinct? 6 26
IP addresses can be stored in a database in any of several ways.  These ways may vary based on the volume of the data.  I was dealing with quite a large amount of data for user authentication purpose, and needed a way to minimize the storage.   …
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
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…

777 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