Solved

Revert changes back in Rowvalidating event of datagridview

Posted on 2010-08-20
5
1,218 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

It seems a simple enough task, yet I see repeated questions asking how to do it: how to pass data between two forms. In this article, I will show you the different mechanisms available for you to do just that. This article is directed towards the .N…
The object model of .Net can be overwhelming at times – so overwhelming that quite trivial tasks often take hours of research. In this case, the task at hand was to populate the datagrid from SQL Server database in Visual Studio 2008 Windows applica…
This is used to tweak the memory usage for your computer, it is used for servers more so than workstations but just be careful editing registry settings as it may cause irreversible results. I hold no responsibility for anything you do to the regist…
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.

911 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

21 Experts available now in Live!

Get 1:1 Help Now