如果未选择日期,如何设置datetimepicker为空值(c#winforms)

时间:2010-06-06 08:24:03

标签: c# winforms datetimepicker

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

6 个答案:

答案 0 :(得分:22)

DateTimePicker s不能设置为null,因为DateTime不能为null,但您可以将它们设置为DateTime.MinValue,这是未初始化{{1}的默认值}}。然后你只需检查DateTime是否如此,将其视为空。

但是,如果您想真正区分何时没有选择任何值,最简单的方法是将dtp.Value = DateTime.MinValue设置为true,然后检查DateTimePicker.ShowCheckBox,如果是,则读取值,否则你将它视为空。

答案 1 :(得分:13)

您可以检查您的绑定源是否提供了一个" null日期"或者您不想显示的日期值。

如果是,请选择自定义格式:

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

添加" CloseUp"或" ValueChanged"事件处理程序重置用户操作的格式:

yourDTP.Format = DateTimePickerFormat.Short

答案 2 :(得分:2)

我喜欢ShowCheckBox选项。我将更进一步,并使用两种扩展方法封装它,以避免重复并具有更清晰的代码:

public static DateTime? NullableValue(this DateTimePicker dtp)
{
    return dtp.Checked ? dtp.Value : (DateTime?)null;
}

public static void NullableValue(this DateTimePicker dtp, DateTime? value)
{
    dtp.Checked = value.HasValue;
    if (value.HasValue) dtp.Value = value.Value;
}

然后,get和set代码如下所示:

dtpSafetyApprover.NullableValue(model.SafetyApproverDate); // set
model.SafetyApproverDate = dtpSafetyApprover.NullableValue(); // get

答案 3 :(得分:1)

最简单的方法是添加一个文本框并将visible属性设置为false。在日期选择器的valuechanged事件上,使用选择器控件的相同日期时间值填充文本框。如果null字符串将值设置为null,则检查文本框的值。

答案 4 :(得分:1)

这就是我解决它的方式。我不能拥有NULLS所以我将1/1/1980 12am定义为MYNULLDATE。我在datetimepicker的文本和值上使用了数据绑定。我没有使用datetimepicker中的复选框。我没有替换从短到自定义的格式。它似乎触发valuechanged事件所以我只使用自定义并更改了自定义格式。我使用了flowpanel和NONBOUND复选框来坐在datetimepicker的前面。

代码示例

   private void dateTimePicker_ValueChanged(object sender, EventArgs e)
    {
        if (sender != null && sender.GetType() == typeof(DateTimePicker))
        {
            if (((DateTimePicker)(sender)).Value == MYNULLDATE)
            {
                ((DateTimePicker)(sender)).CustomFormat = " ";
                checkBoxDate.Checked = false;
            }
            else
            {
                ((DateTimePicker)(sender)).CustomFormat = "M/d/yyyy";
                checkBoxDate.Checked = true;
            }
        }

    }

在非绑定复选框

  private void checkBoxDate_Click(object sender, EventArgs e)
    {
        if (bindingSource != null && bindingSource.Current != null &&
             bindingSource.Current.GetType() == typeof(MyRecord))
        {
            MyRecord a = (MyRecord)bindingSource.Current;
            if (checkBoxDate.Checked == false)
            {
                a.Date = MYNULLDATE;
                dateTimePicker.Enabled = false;
            }
            else
            {
                if (a.Date == null || a.Date == MYNULLDATE)
                {
                    dateTimePicker.Enabled = true;
                    a.Date = DateTime.Now;
                }
            }
            bindingSource.ResetBindings(false);
        }
    }

答案 5 :(得分:0)

使用Vs 2015并且没有运气绑定DateTimePicker控件。最简单的事情似乎就是没有绑定它 - 将对象的值传递给控件,​​手动将控件传递给对象。几行代码将为您节省很多麻烦......

private void BindData()
{
    // can't bind the datetimepicker, so handle it manually...
    if (o.myDate == null)
    {
        dtDatePicker.Checked = false;
    }
    else
    {
        dtDatePicker.Checked = true;
        dtDatePicker.Value = o.myDate.Value;
    }
}

private void Save()
{
    if (dtDatePicker.Checked)
    {
        o.myDate = dtDatePicker.Value;
    }
    else
    {
        o.myDate = null;
    }
}