как установить datetimepicker с нулевым значением если дата, не выбранная (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;
            }
        }
    }
}

Править

я нашел хорошее решение здесь

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

другое идеальное решение здесь

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

16
задан Aaron W. 3 July 2012 в 12:40
поделиться

1 ответ

DateTimePickerы не могут быть установлены на null, потому что DateTime не может быть null, но вы можете установить их на DateTime.MinValue, что является значением по умолчанию для неинициализированного DateTime. А затем вы просто проверяете, если dtp.Value = DateTime.MinValue, и если да, то рассматриваете его как null.

Однако, если вы хотите действительно различать, когда значение не было выбрано, самый простой способ - установить DateTimePicker.ShowCheckBox в true, а затем проверить dtp.Checked и если это true, вы считываете значение, иначе вы рассматриваете его как null.

23
ответ дан 30 November 2019 в 16:09
поделиться
Другие вопросы по тегам:

Похожие вопросы: