DateTime Picker and Null

4 posts, 0 answers
  1. Gary
    Gary avatar
    3 posts
    Member since:
    Jun 2011

    Posted 01 Nov 2011 Link to this post

    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
  2. Peter
    Admin
    Peter avatar
    1148 posts

    Posted 03 Nov 2011 Link to this post

    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.

  3. UI for WinForms is Visual Studio 2017 Ready
  4. Denis Cilliers
    Denis Cilliers avatar
    62 posts
    Member since:
    Jan 2004

    Posted 26 Apr 2013 Link to this post

    how would we do this in VB.Net?
  5. Peter
    Admin
    Peter avatar
    1148 posts

    Posted 01 May 2013 Link to this post

    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.
Back to Top