This is a migrated thread and some comments may be shown as answers.

DateTime Picker and Null

3 Answers 277 Views
Calendar, DateTimePicker, TimePicker and Clock
This is a migrated thread and some comments may be shown as answers.
Gary
Top achievements
Rank 1
Gary asked on 01 Nov 2011, 01:37 PM
I am deriving a custom control from the RadDateTimePicker (version: 2011.1.11.419 or Q1 2011) trying to get it to work with null values. We need it to display an empty string when bound to a null value. For the most part this is working with the code below.

public new DateTime? Value
{
    get 
    {
        if (_value.HasValue)
            return _value.Value;
        return _value; 
    }
    set
    {
        if (_value != value)
        {
            _value = value;
              
            OnValueChanged(new EventArgs());
        }
    }
}
public new DateTime? NullDate
{
    get { return _nullDate; }
    set { _nullDate = null; }
}
void AmDateTextBox_ValueChanged(object sender, System.EventArgs e)
{
    if (this.Value == null)
    {
        this.SetToNullValue();
    }
    else
    {
          
        this.DateTimePickerElement.TextBoxElement.TextBoxItem.Text = this.Value.Value.ToShortDateString();
    }
}

If I am viewing a record with a date value and change to another record with a date value all is well. If I then change to a record with a null date it displays fine. But when I change back to a record with a date value nothing is displayed. I would also like to remove the hard coded format in the Value Changed event handler. I am binding RadDateTimePicker.Value to a DateTime? field on Csla objects not a data table.

Thanks,

grj

3 Answers, 1 is accepted

Sort by
0
Peter
Telerik team
answered on 03 Nov 2011, 03:57 PM
Hello Gary,

Thank you for writing.

Your approach is correct and should work, but simple data binding requires OnNotifyPropertyChanged and an event named after the name of the property of interest, for example ValueChanged event.
Please refer to my test project that demonstrates RadDateTime inheritor with simple data binding with my business object.

I hope this helps.

Kind regards,
Peter
the Telerik team

Q2’11 SP1 of RadControls for WinForms is available for download (see what's new); also available is the Q3'11 Roadmap for Telerik Windows Forms controls.

0
Denis Cilliers
Top achievements
Rank 1
answered on 26 Apr 2013, 03:35 PM
how would we do this in VB.Net?
0
Peter
Telerik team
answered on 01 May 2013, 08:19 AM
Hello Denis,

Please, find the attached the VB version of this project.

In future, when you need conversion between C# and VB, you can use our free online converter: http://converter.telerik.com/.

I hope you find this useful.

Greetings,
Peter
the Telerik team
WinForms Q1 2013 boasts PivotGrid, PDF Viewer, Chart enhancements and more. Check out all of the latest highlights.
Tags
Calendar, DateTimePicker, TimePicker and Clock
Asked by
Gary
Top achievements
Rank 1
Answers by
Peter
Telerik team
Denis Cilliers
Top achievements
Rank 1
Share this question
or