DataBinding Issues using IEditableObject


  • I am binding my DataGridView to a collection (Collection that implements IEditablleObject and IBindingList)

    I am using the following classes for IEditableObject and IBindingList


    On a windows form named form1 I have a datagridview (named DataGridView1, Three text boxes named txtID, txtFirstName and TxtLastName and two button named btnClear and btnCommit

    If a user makes a change (either in grid or textboxes and changes his/her mind clear button put the original value back

    Commit button should commit changes to the underlying object (datasource)

    Public Class Form1

    Private WithEvents bs  as New BindingSource
    Private CustomersList AS  CustomersList
    Private bInit AS Boolean = False

    Private Sub Form1_Load( ByVal sender As System.Object,  ByVal e As System.EventArgs) Handles MyBase.Load
           bInit = True
           CustomersList = New CustomersList()
           bs.DataSource =  CustomersList
           DataGridView1.DataSource = bs



           txtLastName.DataBindings.Add("Text", bs, "LastName")
           bInit = False
    End Sub

    'Clear Button to clear any changes
    Private Sub Clear_Click( ByVal sender As System.Object,  ByVal e As System.EventArgs) Handles btnClear.Click
    End Sub

    'Commit Button that commit changes to underlying object (DataSource)
    Private Sub btnCommit_Click( ByVal sender As System.Object,  ByVal e As System.EventArgs) Handles btnCommit.Click
            Dim cm as CurrencyManager = Ctype(DataGridView1.BindingContext(bs),CurrencyManager)
            'call to database update code goes here
    End Sub

    End Class
    Here is my Question?
    When user navigates the grid and if he/she has made any changes to the currenlty selected item in the grid I want to prompt them to commitr or revert?

    The following works but I do not know any easy way to find whether current object is dirty (changed) or not when user navigates between the datagridview rows?

    Private Sub bs_PositionChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles bs.PositionChanged
           If NOT bInit Then
                If CurrentlyselectedItem inthe grid is Changed Then
                    If MessageBox.Show("Save changes?", "Confirm changes", MessageBoxButtons.YesNo, MessageBoxIcon.Warning) = Windows.Forms.DialogResult.Yes Then
                          CustomersList.Item(DataGridView1.CurrentRow.Index).IsChanged = False
                    End If
                End If
           End If

         End Sub

    Monday, October 02, 2006 4:17 PM