[RESOLVED] databound textbox.
I am having a little problem with a databinding.
I am using the following to display a date in a textbox on a form:
Code:
.txtOpen.DataBindings.Add(New Binding("Text", frmEvent._MasterBase5_0DataSet, "tblEventMaster.dteOpen"))
This is to display a date field set as a short date. I do get a short date in the textbox, but it also includes the time. It is my guess that the problem is from the "Text", but I have no way of knowing that for sure. So on the assumption that I am correct, what should "Text" be replaced with?
Re: [RESOLVED] databound textbox.
This is truly some detailed and esoteric crap. But as I long ago learned, details kill. It seems like the deeper I dive into this the worse it gets. Now that I have the whining out, this is actually all pretty cool. I am getting to the point where I can come close to understanding what all is going on, but I still find the designer to be a little scary
Thanks JM
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.
Re: [RESOLVED] databound textbox.
Looks pretty cool JM, but this is going to take some time for me to digest and wrap my head around. From what I can already understand this really looks clean to me and probably much more efficient. I do thank you.