How to tell if user has modified data using bindingsource?

后端 未结 12 2182
南方客
南方客 2021-01-05 06:05

I have a DataGridView bound to a bindingsource which is bound to a List. The user clicks a row that goes to a form with textboxes, etc. The textboxes a

12条回答
  •  佛祖请我去吃肉
    2021-01-05 06:28

    What I always do is to capture the individual "changed" events of the controls. In the below example I used a tabcontrol in this example. The Try/Catch is a dirty solution for not having to deal with all kinds of exceptions ;-)

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        '
        ' some code        
        '
        BindingNavigatorSaveItem.Enabled = False
        For Each tabctl As Control In Me.TabControl1.Controls
            For Each ctl As Control In tabctl.Controls
                Try
                    If ctl.GetType Is GetType(TextBox) Then
                        AddHandler DirectCast(ctl, TextBox).TextChanged, AddressOf GenDataChanged
                    ElseIf ctl.GetType Is GetType(NumericUpDown) Then
                        AddHandler DirectCast(ctl, NumericUpDown).ValueChanged, AddressOf GenDataChanged
                    ElseIf ctl.GetType Is GetType(ComboBox) Then
                        AddHandler DirectCast(ctl, ComboBox).SelectedValueChanged, AddressOf GenDataChanged
                    ElseIf ctl.GetType Is GetType(CheckBox) Then
                        AddHandler DirectCast(ctl, CheckBox).CheckStateChanged, AddressOf GenDataChanged
                    End If
                Catch ex As Exception
                End Try
            Next
        Next
    End Sub
    
    Private Sub GenDataChanged(sender As System.Object, e As System.EventArgs)
        BindingNavigatorSaveItem.Enabled = True
    End Sub
    

提交回复
热议问题