Groups | Blog | Home
all groups > dotnet windows forms databinding > november 2007 >

dotnet windows forms databinding : Binding an Object Property with DateTimePicker


PG
11/16/2007 12:59:01 PM
I'm binding an business object's property (DateTime datatype) with a
DateTimePicker control on my windows form using below mentioned code. While
loading the form, the control is getting its value correctly by hitting the
Property Get, but when I change the date on the control, its not calling the
Property Set, instead it calls Property Get, as a result of which, the data I
entered is not getting saved. I tried to bind the value using following
code, neither of it worked. Am I missing something?

'1)
dtpTest.DataBindings.Add("Value", _businessObj, "DeadLineDate", False,
DataSourceUpdateMode.OnPropertyChanged)

'2)
dtpTest.DataBindings.Add("Value", _businessObj, "DeadLineDate", False)

PG
11/16/2007 1:14:00 PM
My business object is a simple object, and its not implementing any
interface, Should it be?

Another Bound ComboBox on the same Windows Form, behaves perfectly as it
supposed to be ( when bound to the BO's Property).

[quoted text, click to view]
Morten Wennevik [C# MVP]
11/18/2007 3:58:03 PM
Hi PG,

There is nothing magical about DateTimePicker.

dtpTest.DataBindings.Add("Value", _businessObj, "DeadLineDate");

should be enough, provided _businessObj.DeadLineDate exists and is of ty=
pe DateTime. However, if there any incompatibilities in the databinding=
, like DeadLineDate is nullable DateTime instead of regular you will see=
the getter called without ever being set. If that is the case, then yo=
u need at least to implement the Binding.Parse event.

protected override void OnLoad(System.EventArgs e)
{
Binding b =3D new Binding("Value", myObj, "Date", true);
b.Parse +=3D new ConvertEventHandler(b_Parse);
dateTimePicker1.DataBindings.Add(b);
}

void b_Parse(object sender, ConvertEventArgs e)
{
e.Value =3D new DateTime?((DateTime)e.Value);
}

Try implementing Binding.Parse and Binding.Format to see what kind of va=
lues are passed and what is expected (compare e.Value.GetType with e.Des=
iredType).



On Fri, 16 Nov 2007 22:14:00 +0100, PG <PG@discussions.microsoft.com> wr=
ote:

[quoted text, click to view]



-- =

Happy coding!
PG
11/19/2007 12:42:01 PM
Thanks Morten,

What you mentioned was absolutely was my situation here. The property on my
business object is of type Nullable datetime.

e.desiredType states it wants the result in Nullable of DateTime, but my
e.type is regular DateTime. I tried to assign the e.value to a variable of
type Nullable DateTime and then reassigned the variable to e.value. While,
the assignment, works perfectly, if i execute it in Immediate window, and
only in immediate window. In other words, the assignment on the Parse Event
does not seem to be working from the code window.

Here is what I have..
Private Sub bind_Parse(ByVal sender As Object, ByVal e As
System.Windows.Forms.ConvertEventArgs) Handles bind.Parse
Dim test As Binding
test = CType(sender, Binding)
Dim val As New Nullable(Of System.DateTime)
val = e.Value

''''******* none of the following assignment work from code
''''******* window but works fine from Immediate window.
'e.Value = DirectCast(val, Nullable(Of System.DateTime))
e.Value = CType(val, Nullable(Of System.DateTime))
e.value = val
End Sub

Any suggestions. I appreciate your help.

[quoted text, click to view]
Morten Wennevik [C# MVP]
11/21/2007 6:55:19 PM
Hi PG,

Hi believe the syntax in VB is something like this

' Converting the DateTime in e.Value to a nullable DateTime
Dim d =3D CType(e.Value, DateTime)
e.Value =3D New Nullable(Of DateTime)(d)

However, I tried to convert my C# sample to VB and had no problems updat=
ing the underlying business object even without converting the type and =
using the parse event so your problem might be something else. VB does =
a whole lot of "magic" behind the scenes you won't find in C# so this mi=
ght be a VB thing.

If you can assemble something small enough to be run as a stand alone pr=
ogram (just the necessary controls, the databinding and a stripped down =
business object) I could take a closer look.




On Mon, 19 Nov 2007 21:42:01 +0100, PG <PG@discussions.microsoft.com> wr=
ote:

[quoted text, click to view]
type DateTime. However, if there any incompatibilities in the databind=
ing, like DeadLineDate is nullable DateTime instead of regular you will =
see the getter called without ever being set. If that is the case, then=
you need at least to implement the Binding.Parse event.
[quoted text, click to view]
values are passed and what is expected (compare e.Value.GetType with e.=
DesiredType).
[quoted text, click to view]



-- =

Happy coding!
AddThis Social Bookmark Button