|
-
Feb 3rd, 2018, 09:17 PM
#11
Re: [RESOLVED] databound textbox.
With regards to using the designer for binding, most people would tend to recommend against it and, in fact, it's probably the main reason that typed DataSets have an undeserved bad name. Setting up binding in the designer means tightly coupling your data access code and your presentation code. Doing so makes things easier for beginners because they can get a lot done with a limited understanding of how it works but more experienced developers prefer to separate their data access code and their presentation code, possibly into separate projects.
The Binding object is actually perfectly logical and simply does the work of moving the data between the data source and the UI and back again that you would otherwise have to write code for yourself. The first three arguments to the Binding constructor in my code, i.e. "Text", Me.PersonBindingSource and "DateOfBirth", specify that Me.PersonBindingSource is the data source and, when applicable, data is to be copied from the "DateOfBirth" property of its current item to the "Text" property of the control and back again. That saves you write code like this:
vb.net Code:
Me.TextBox1.Text = DirectCast(Me.PersonBindingSource.Current, DateRowView)("DateOfBirth").ToString()
and like this:
vb.net Code:
DirectCast(Me.PersonBindingSource.Current, DateRowView)("DateOfBirth") = Date.Parse(Me.TextBox1.Text)
The fourth and seventh arguments, i.e. True and "d", specify that the dates displayed should use the system short date format, so saving you from writing code like this:
vb.net Code:
Me.TextBox1.Text = DirectCast(Me.PersonBindingSource.Current, DateRowView)("DateOfBirth").ToString("d")
and like this:
vb.net Code:
DirectCast(Me.PersonBindingSource.Current, DateRowView)("DateOfBirth") = Date.ParseExact(Me.TextBox1.Text, "d", Nothing)
The fifth argument, i.e. System.Windows.Forms.DataSourceUpdateMode.OnValidation, specifies that the data should be copied from the control to the data source on the Validated event rather than on the TextChanged event, which is a decision you'd have to make too, if you were writing the code yourself. The code inside a Binding object is reasonably complex because it has to handle many more situations that you would write code for yourself, but it still just does the very same things that you would do if you were moving the data back and forth yourself.
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|