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)
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] "PG" wrote: > 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) >
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] > 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). > > "PG" wrote: > >> 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 hitti= ng the >> Property Get, but when I change the date on the control, its not call= ing the >> Property Set, instead it calls Property Get, as a result of which, th= e data I >> entered is not getting saved. I tried to bind the value using follow= ing >> 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= ) >> >> >
-- = Happy coding!
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]" wrote: > Hi PG, > > There is nothing magical about DateTimePicker. > > dtpTest.DataBindings.Add("Value", _businessObj, "DeadLineDate"); > > should be enough, provided _businessObj.DeadLineDate exists and is of type 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 you need at least to implement the Binding.Parse event. > > protected override void OnLoad(System.EventArgs e) > { > Binding b = new Binding("Value", myObj, "Date", true); > b.Parse += new ConvertEventHandler(b_Parse); > dateTimePicker1.DataBindings.Add(b); > } > > void b_Parse(object sender, ConvertEventArgs e) > { > e.Value = new DateTime?((DateTime)e.Value); > } > > Try implementing Binding.Parse and Binding.Format to see what kind of values are passed and what is expected (compare e.Value.GetType with e.DesiredType). > > > > On Fri, 16 Nov 2007 22:14:00 +0100, PG <PG@discussions.microsoft.com> wrote: > > > 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). > > > > "PG" wrote: > > > >> 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) > >> > >> > > > > > > -- > Happy coding! > Morten Wennevik [C# MVP]
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] > 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 variabl= e of > type Nullable DateTime and then reassigned the variable to e.value. W= hile, > 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.P= arse > Dim test As Binding > test =3D CType(sender, Binding) > Dim val As New Nullable(Of System.DateTime) > val =3D e.Value > > ''''******* none of the following assignment work from code > ''''******* window but works fine from Immediate window. > 'e.Value =3D DirectCast(val, Nullable(Of System.DateTime)) > e.Value =3D CType(val, Nullable(Of System.DateTime)) > e.value =3D val > End Sub > > Any suggestions. I appreciate your help. > > "Morten Wennevik [C# MVP]" wrote: > >> Hi PG, >> >> There is nothing magical about DateTimePicker. >> >> dtpTest.DataBindings.Add("Value", _businessObj, "DeadLineDate"); >> >> should be enough, provided _businessObj.DeadLineDate exists and is of=
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] >> >> 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=
values are passed and what is expected (compare e.Value.GetType with e.= DesiredType). [quoted text, click to view] >> >> >> >> On Fri, 16 Nov 2007 22:14:00 +0100, PG <PG@discussions.microsoft.com>= wrote: >> >> > 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). >> > >> > "PG" wrote: >> > >> >> I'm binding an business object's property (DateTime datatype) with= a >> >> DateTimePicker control on my windows form using below mentioned co= de. While >> >> loading the form, the control is getting its value correctly by hi= tting the >> >> Property Get, but when I change the date on the control, its not c= alling 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 fol= lowing >> >> code, neither of it worked. Am I missing something? >> >> >> >> '1) >> >> dtpTest.DataBindings.Add("Value", _businessObj, "DeadLineDate", Fa= lse, >> >> DataSourceUpdateMode.OnPropertyChanged) >> >> >> >> '2) >> >> dtpTest.DataBindings.Add("Value", _businessObj, "DeadLineDate", Fa= lse) >> >> >> >> >> > >> >> >> >> -- >> Happy coding! >> Morten Wennevik [C# MVP] >> >
-- = Happy coding!
Don't see what you're looking for? Try a search.
|