Winforms 是否有可以绑定到的可为空的日期选择器?
我正在寻找一个像微软提供的一样的日期选择器,但它不支持空值,因为它与数据库上的一个可空字段绑定在一起,这是不可接受的Winforms 是否有可以绑定到的可为空的日期选择器?,winforms,data-binding,.net-3.5,datepicker,nullable,Winforms,Data Binding,.net 3.5,Datepicker,Nullable,我正在寻找一个像微软提供的一样的日期选择器,但它不支持空值,因为它与数据库上的一个可空字段绑定在一起,这是不可接受的 我发现了,但根据页面底部的评论,它在绑定到数据库方面存在问题。在我的项目中,我也继承了一个,但它也有类似的问题,有时它显示了价值,有时它没有。有人知道一个可以工作吗?为什么不使用客户端日期选择器来填充文本字段呢。如果文本字段为空,则日期为空,否则转换值 jQuery有一个很好的、易于使用的日期选择器 > P>使用日期选择器填充文本框,如果希望字段为空,只需擦除文本框的内容,然后相
我发现了,但根据页面底部的评论,它在绑定到数据库方面存在问题。在我的项目中,我也继承了一个,但它也有类似的问题,有时它显示了价值,有时它没有。有人知道一个可以工作吗?为什么不使用客户端日期选择器来填充文本字段呢。如果文本字段为空,则日期为空,否则转换值
jQuery有一个很好的、易于使用的日期选择器 > P>使用日期选择器填充文本框,如果希望字段为空,只需擦除文本框的内容,然后相应地处理空白输入。
这还提供了一个额外的好处,即允许用户根据自己的选择键入日期。Smart FieldPackEditor有一个可为空的日期选择器。我相信它能做你需要的一切。我希望在我处理这类事情的时候能有这个。我仍然记得我用微软的datepicker控件实现的所有变通方法。啊
这个似乎有用,我的一个同事有:
using System;
using System.Windows.Forms;
namespace CustomControls
{
public class NullableBindableDateTimePicker : System.Windows.Forms.DateTimePicker
{
private Boolean isNull = false;
private DateTimePickerFormat baseFormat = DateTimePickerFormat.Short;
private Boolean ignoreBindOnFormat = false;
public NullableBindableDateTimePicker()
{
this.Format = baseFormat;
if (baseFormat == DateTimePickerFormat.Custom) this.CustomFormat = " ";
}
public Boolean IsNull
{
get { return isNull; }
set
{
isNull = value;
this.Checked = value;
}
}
//TODO: Add BaseCustomFormat
public DateTimePickerFormat BaseFormat
{
get { return baseFormat; }
set { baseFormat = value; }
}
public object BindMe
{
get
{
if (IsNull) return System.DBNull.Value;
else return base.Value;
}
set
{
//String s = this.Name;
if (ignoreBindOnFormat) return;
if (System.Convert.IsDBNull(value))
{
// for some reason setting base.format in this.format calls set BindMe.
// we need to ignore the following call
ignoreBindOnFormat = true;
this.Format = DateTimePickerFormat.Custom;
ignoreBindOnFormat = false;
this.CustomFormat = " ";
IsNull = true;
}
else
{
ignoreBindOnFormat = true;
this.Format = baseFormat;
ignoreBindOnFormat = false;
if (baseFormat == DateTimePickerFormat.Custom) this.CustomFormat = " ";
IsNull = false;
base.Value = (DateTime)value;
}
}
}
protected override void OnKeyDown(KeyEventArgs e)
{
base.OnKeyDown(e);
if (e.KeyCode == Keys.Delete)
{
this.BindMe = DBNull.Value;
}
}
protected override void OnCloseUp(EventArgs eventargs)
{
base.OnCloseUp(eventargs);
BindMe = base.Value;
}
}
}
抱歉,这是针对winforms应用程序的,不是web。当他在谈论C时,假设他在谈论web应用程序是很愚蠢的。哦,是的,很抱歉。现在投票最多的答案是使用相同的概念,所以显然这个概念是有效的。我的头被困在互联网上,因为我每天都在那里度过。一定有人以前遇到过这个问题并解决了它,可能比我能解决的要好得多。我找过了,没找到。我的问题是其他人是否找到了有效的方法。同意。或者有一个复选框来启用和禁用它。不要把事情复杂化。最好的解决方案往往是最简单的解决方案的倍数。