Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Windows Forms / WinForm Data Binding / March 2005

Tip: Looking for answers? Try searching our database.

Data Binding and validating data

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
post messages - 26 Mar 2005 09:01 GMT
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.

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.