none
DataBinding Issues using IEditableObject

    Question

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

    I am using the following classes for IEditableObject and IBindingList
    Customer
    http://msdn2.microsoft.com/en-us/library/system.componentmodel.ieditableobject.aspx

    CustomerList
    http://msdn2.microsoft.com/en-us/library/system.componentmodel.ibindinglist.aspx

    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

           txtID.DataBindings.Clear()
           txtID.DataBindings.Add("Text",bs,"ID")

           txtFirstName.DataBindings.Clear()
           txtFirstName.DataBindings.Add("Text",bs,"FirstName")

           txtLastName.DataBindings.Clear()
           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
       bs.CancelEdit()
    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
            bs.EndEdit
            Dim cm as CurrencyManager = Ctype(DataGridView1.BindingContext(bs),CurrencyManager)
            cm.Refresh()
            '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
                          bs.EndEdit
                    Else
                          CustomersList.Item(DataGridView1.CurrentRow.Index).IsChanged = False
                          bs.CancelEdit
                        
                    End If
                End If
            
           End If

         End Sub

    Monday, October 02, 2006 4:17 PM