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?
+2


a source to share


5 answers


Unfortunately I think you can write your own custom control that includes both, with which you can bind data. This is how we solved the same problem.



+1


a source


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.

+2


a source


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);

      

+1


a source


I know this is a little outdated, but I used the solution I found here:

http://office.microsoft.com/en-us/infopath-help/display-the-date-and-time-in-separate-controls-HA010082330.aspx

This works great and is not a software solution

+1


a source


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.

0


a source







All Articles