Data Binding and validating data

From: post messages (post.messages_at_gmail.com)
Date: 03/26/05

  • Next message: post.messages_at_gmail.com: "Re: Data Binding and validating data"
    Date: 26 Mar 2005 00:01:00 -0800
    
    

    I have a few issues with databinding.

    First of all, I read so much about how painful it seemed to be for
    people that I initially started out not using it. As in, if I do
    things manually, at least I know what is going on.

    However, I liked what I read about the ErrorProvider and the
    SetRowError and SetColumnError. I thought it would be a great way to
    do business rule validation and be able to report back to the
    frontend. So that put me on the slippery slop of DataBinding.

    One of my more puzzling issues:
    1) I have a textbox bound to a decimal field in a datarow. If I type
    an 'x' in the textbox and then leave focus the text box is returned to
    the value before I typed 'x' and the program resumes normal operation.

    I have an event handler on Binding.Parse. I can see 'x' there,
    however, I don't know how to stop databinding from undoing the data. I
    want to show an error and give the user a chance to see the incorrect
    data. I tried with various combinations without success:
    Me.BindingContext(sender).EndCurrentEdit, CancelCurrentEdit and
    SuspendBinding().

    EndCurrentEdit in Parse was interesting as it put me into an infinite
    loop retesting the bad data.

    Here is some code:
        Private Sub ErrorProvider_Load(ByVal sender As System.Object,
    ByVal e As System.EventArgs) Handles MyBase.Load

            ds = New DataSet
            Dim dt As New DataTable("ProductChoice")
            ds.Tables.Add(dt)
            Me.SqlDataAdapter1.Fill(ds)

            Dim b As Binding = New Binding _
                ("Text", ds.Tables("ProductChoice"), "Discount")

            AddHandler b.Parse, AddressOf ParseCurrencyStringToDecimal
            TextBox1.DataBindings.Add(b)

            Dim bmCustomers As BindingManagerBase =
    Me.BindingContext(ds.Tables("ProductChoice"))
            'AddHandler bmCustomers.CurrentChanged, AddressOf
    Current_Changed
            ' Add the delegate for the PositionChanged event.
            'AddHandler bmCustomers.PositionChanged, AddressOf
    Position_Changed

            Me.BindingContext(ds.Tables("ProductChoice")).Position = 0

        End Sub

        Private Sub ParseCurrencyStringToDecimal(ByVal sender As Object,
    ByVal cevent As ConvertEventArgs)

            Try
                ' This is where I can see 'x' but can not stop
    databindings from resetting back to the original data.

            Catch ex As Exception
                Me.ErrorProvider1.SetError(DirectCast(sender,
    Binding).Control, "incorrect")
                'DirectCast(sender,
    Binding).BindingManagerBase.CancelCurrentEdit()
                'DirectCast(sender,
    Binding).BindingManagerBase.SuspendBinding()
                'DirectCast(sender,
    Binding).BindingManagerBase.EndCurrentEdit()

                'Throw
            End Try

        End Sub


  • Next message: post.messages_at_gmail.com: "Re: Data Binding and validating data"