How to Data Data bind a double DateTimePicker to a single DateTime object
I have a simple form with two DateTimePicker controls, one for the date and one for the time. The point is that these two controls must represent a single point in time. So I would like to bind them to a single DateTime property on my form (for simplicity's sake). I did the following:
// Start is a DateTime property on the form
_StartDate.DataBindings.Add("Value", this, "Start");
_StartTime.DataBindings.Add("Value", this, "Start");
But connecting to the "ValueChanged" event gives mixed results ... Sometimes I get exactly what I want, sometimes the updates to the property are "sluggish". I figured this way of splitting the DateTimePicker into two was pretty common. So how do you do it?
Update: There may be several questions:
- How to bind DateTimePicker (Format: Date) to DateTime attribute on form?
- Then, how do I bind another DateTimePicker (Format: Time) to the same property?
- I am using Visual Studio Express 2008 (.NET 3.5) and I am apparently getting ValueChanged events from DateTimePickers before , is the value changed?
a source to share
I run into this problem occasionally and I use a somewhat ugly technique. Instead of binding directly to a property, I create two additional properties for the model.
//The data I want to bind to
public DateTime Something { get; set; }
//Two new properties
public DateTime SomethingDate { /*...*/ }
public DateTime SomethingTime { /*...*/ }
Sections of get
each should just return Something
. Sections set
are where you implement a tiny bit of logic to ensure that it is Something
always accurately reflected.
public DateTime SomethingDate
{
get
{
return this.Something;
}
set
{
this.Something = value.Date.Add(this.Something.TimeOfDay);
}
}
public DateTime SomethingTime
{
get
{
return this.Something;
}
set
{
this.Something = this.Something.Date.Add(value.TimeOfDay);
}
}
Now your property Something
will always correctly reflect the respective date and time of the two controls, only updating the value with information from each we care about.
a source to share
You cannot directly bind a member of the DateTime structure to the binding class, for that you need to wrap the DateTime structure and implement the PropertyChanged event of the INotifyPropertyChanged interface like this:
public class DateTimeWrapper : INotifyPropertyChanged
{
private DateTime _dateTime;
public event PropertyChangedEventHandler PropertyChanged;
public void OnPropertyChanged()
{
PropertyChangedEventHandler handler = PropertyChanged;
if (handler != null) handler(this, new PropertyChangedEventArgs("Value"));
}
public DateTime Value
{
get { return _dateTime; }
set
{
_dateTime = value;
OnPropertyChanged();
}
}
}
after which you can use the Bindig object like this:
var binding = new Binding("Value", _time, "Value", true);
binding.ControlUpdateMode = ControlUpdateMode.OnPropertyChanged;
dateTimePicker1.DataBindings.Add(binding);
a source to share
I have a public property DateTime
in my WinForm named EpDayOnly. Then I bind the datetimepicker to the property by writing:
myDateTimePicker.DataBindings.Add("Value", this, "EpDayOnly", false, DataSourceUpdateMode.OnPropertyChanged);
When I open the form, I set the datetime property by writing for example
myNewForm.EpDayOnly=DateTime.Parse("5/5/2012");
and this is reflected in the datetimepicker control.
a source to share