Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to set datetimepicker with null value if date not selected(c# winforms)

Binding b = new Binding( "Value", person, "BdayNullable", true );
dtBirthdayNullable.DataBindings.Add( b );
b.Format += new ConvertEventHandler( dtBirthdayNullable_Format );

b.Parse += new ConvertEventHandler( dtBirthdayNullable_Parse );

void dtBirthdayNullable_Format( object sender, ConvertEventArgs e )
{
    // e.Value is the object value, we format it to be what we want to show up in the control

    Binding b = sender as Binding;
    if ( b != null )
    {
        DateTimePicker dtp = (b.Control as DateTimePicker);
        if ( dtp != null )
        {
            if ( e.Value == DBvalue.value )
            {
                dtp.ShowCheckBox = true;
                dtp.Checked = false;

                // have to set e.Value to SOMETHING, since it's coming in as NULL
                // if i set to DateTime.Today, and that's DIFFERENT than the control's current 
                // value, then it triggers a CHANGE to the value, which CHECKS the box (not ok)
                // the trick - set e.Value to whatever value the control currently has.  
                // This does NOT cause a CHANGE, and the checkbox stays OFF.
                e.Value = dtp.Value;    
            }
            else
            {
                dtp.ShowCheckBox = true;
                dtp.Checked = true;
                // leave e.Value unchanged - it's not null, so the DTP is fine with it.
            }
        }
    }
}
void dtBirthdayNullable_Parse( object sender, ConvertEventArgs e )
{
    // e.value is the formatted value coming from the control.  
    // we change it to be the value we want to stuff in the object.

    Binding b = sender as Binding;
    if ( b != null )
    {
        DateTimePicker dtp = (b.Control as DateTimePicker);
        if ( dtp != null )
        {
            if ( dtp.Checked == false )
            {
                dtp.ShowCheckBox = true;
                dtp.Checked = false;
                e.Value = DBvalue.Value
            }
            else
            {
                DateTime val = Convert.ToDateTime( e.Value );
                e.Value =val;
            }
        }
    }
}

EDIT

i found a good solution here

http://blogs.interknowlogy.com/danhanan/archive/2007/01/21/10847.aspx

another perfect solution here

http://www.mofeel.net/70-microsoft-public-dotnet-framework-windowsforms/8806.aspx

like image 260
shmandor Avatar asked Jun 06 '10 08:06

shmandor


People also ask

How do you make a DatePicker blank?

You just need to set property valueDefault={null} and the DatePicker will be blank.

How can I make a DateTimePicker display an empty string?

Set datetimepicker FORMAT property to custom. set CUSTOM FORMAT property to empty string " ". set its TAG to 0 by default.

How check DatePicker is null?

The DatePicker class has a property, SelectedDate, which enable you to get or set the selected date. If there is none selected, it means its value will be null.


2 Answers

DateTimePickers can't be set to null because DateTime can't be null, but you can set them to DateTime.MinValue which is the default value for an uninitialized DateTime. And then you just check if the dtp.Value = DateTime.MinValue and if so, treat it as null.

However, if you want to really distinguish when no value has been selected, the easiest way is to set DateTimePicker.ShowCheckBox to true, and then you check dtp.Checked and if it's true, you read the value, otherwise you treat it as a null.

like image 115
Hans Olsson Avatar answered Oct 16 '22 09:10

Hans Olsson


You can check if your binding source delivers a "null date" or a date value that you don't want to display.

If so, select custom format:

yourDTP.Format = DateTimePickerFormat.Custom
yourDTP.CustomFormat = " "

Add a "CloseUp" or "ValueChanged" event handler to reset the format on user action:

yourDTP.Format = DateTimePickerFormat.Short
like image 32
AvS Avatar answered Oct 16 '22 07:10

AvS