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
post.messages@gmail.com - 26 Mar 2005 21:59 GMT
Thanks for the confirmation! I will continue with my manual way and use
SetError and manually read Row and Column error values from the
DataSet. I hope VS2005 has a more usable system. It sure would save
time if it could be trusted.