首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如果日期未选定,如何设置空值的日期选择器(c# winforms)

如果日期未选定,如何设置空值的日期选择器(c# winforms)
EN

Stack Overflow用户
提问于 2010-06-06 08:24:03
回答 6查看 84.9K关注 0票数 17
代码语言:javascript
复制
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

EN

回答 6

Stack Overflow用户

发布于 2010-06-06 09:42:51

DateTimePicker不能设置为null,因为DateTime不能是null,但是可以将它们设置为DateTime.MinValue,这是未初始化的DateTime的默认值。然后您只需检查dtp.Value = DateTime.MinValue是否为空,如果是,则将其视为null。

但是,如果要真正区分何时没有选择值,最简单的方法是将DateTimePicker.ShowCheckBox设置为true,然后检查dtp.Checked,如果为真,则读取该值,否则将其视为null。

票数 28
EN

Stack Overflow用户

发布于 2013-03-15 10:36:08

您可以检查绑定源是否传递“空日期”或不想显示的日期值。

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

代码语言:javascript
复制
yourDTP.Format = DateTimePickerFormat.Custom
yourDTP.CustomFormat = " "

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

代码语言:javascript
复制
yourDTP.Format = DateTimePickerFormat.Short
票数 18
EN

Stack Overflow用户

发布于 2013-07-11 19:52:01

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

代码语言:javascript
复制
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代码如下所示:

代码语言:javascript
复制
dtpSafetyApprover.NullableValue(model.SafetyApproverDate); // set
model.SafetyApproverDate = dtpSafetyApprover.NullableValue(); // get
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/2983563

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档