自己写的一个用于解决DateTimePicker的值无法为Null的控件.不过有问题.需要请各位帮忙看一下问题出在哪.
我的代码如下
/// <summary>
/// 用于处理DBNull问题的DateTimePicker
/// </summary>
public class MDateTimePicker : System.Windows.Forms.DateTimePicker
{
/// <summary>
/// 用于保存本身的值.可以为Null
/// </summary>
private object dateValue;
/// <summary>
/// 此控件使用DateTimePicker 本身的CheckBox来控制是否为DBNull,如果Checked==false;则为DBNull
/// 所以要让此控件显示CheckBox;
/// </summary>
public MDateTimePicker()
{
this.ShowCheckBox = true;
}
/// <summary>
/// 隐藏基类的Value
/// </summary>
public new object Value
{
get
{
return this.dateValue;
}
set
{
if (value != DBNull.Value)
{
base.Value = Convert.ToDateTime(value);
this.dateValue = value;
}
else
{
this.dateValue = null;
}
valueChanged();
}
}
protected override void OnBindingContextChanged(EventArgs e)
{
//base.OnBindingContextChanged(e);
//valueChanged();
}
/// <summary>
/// 当值改变时,修改日期的显示格式
/// </summary>
/// <param name="eventargs"></param>
protected override void OnValueChanged(EventArgs eventargs)
{
if (this.Checked)
{
this.Format = DateTimePickerFormat.Long;
}
}
protected override void OnTextChanged(EventArgs e)
{
base.OnTextChanged(e);
valueChanged();
}
/// <summary>
/// 当值改变时,修改Checked属性,并修改Format为" ",这样控件就不会显示日期了.看起来像Null
/// </summary>
private void valueChanged()
{
if (this.Value == DBNull.Value || this.Value==null)
{
this.Checked = false;
this.Format = DateTimePickerFormat.Custom;
this.CustomFormat = " ";
}
else
{
this.Checked = true;
this.Format = DateTimePickerFormat.Long;
this.dateValue = base.Value;
}
}
/// <summary>
/// 当按下Delete时,将值修改为Null
/// </summary>
/// <param name="e"></param>
protected override void OnKeyDown(KeyEventArgs e)
{
if (e.KeyCode == Keys.Delete)
{
this.Value = DBNull.Value;
}
}
}
/// 用于处理DBNull问题的DateTimePicker
/// </summary>
public class MDateTimePicker : System.Windows.Forms.DateTimePicker
{
/// <summary>
/// 用于保存本身的值.可以为Null
/// </summary>
private object dateValue;
/// <summary>
/// 此控件使用DateTimePicker 本身的CheckBox来控制是否为DBNull,如果Checked==false;则为DBNull
/// 所以要让此控件显示CheckBox;
/// </summary>
public MDateTimePicker()
{
this.ShowCheckBox = true;
}
/// <summary>
/// 隐藏基类的Value
/// </summary>
public new object Value
{
get
{
return this.dateValue;
}
set
{
if (value != DBNull.Value)
{
base.Value = Convert.ToDateTime(value);
this.dateValue = value;
}
else
{
this.dateValue = null;
}
valueChanged();
}
}
protected override void OnBindingContextChanged(EventArgs e)
{
//base.OnBindingContextChanged(e);
//valueChanged();
}
/// <summary>
/// 当值改变时,修改日期的显示格式
/// </summary>
/// <param name="eventargs"></param>
protected override void OnValueChanged(EventArgs eventargs)
{
if (this.Checked)
{
this.Format = DateTimePickerFormat.Long;
}
}
protected override void OnTextChanged(EventArgs e)
{
base.OnTextChanged(e);
valueChanged();
}
/// <summary>
/// 当值改变时,修改Checked属性,并修改Format为" ",这样控件就不会显示日期了.看起来像Null
/// </summary>
private void valueChanged()
{
if (this.Value == DBNull.Value || this.Value==null)
{
this.Checked = false;
this.Format = DateTimePickerFormat.Custom;
this.CustomFormat = " ";
}
else
{
this.Checked = true;
this.Format = DateTimePickerFormat.Long;
this.dateValue = base.Value;
}
}
/// <summary>
/// 当按下Delete时,将值修改为Null
/// </summary>
/// <param name="e"></param>
protected override void OnKeyDown(KeyEventArgs e)
{
if (e.KeyCode == Keys.Delete)
{
this.Value = DBNull.Value;
}
}
}
我的问题是:
我在表单中.是这样写的
private MDateTimePicker dtpPurchaseDate;
this.dtpPurchaseDate = new WinUI.MDateTimePicker();
DataSet ds=new PurchaseMainBLL().GetAllList();
this.bindingSource1.DataSource = ds;
this.bindingSource1.DataMember = ds.Tables[0].TableName;
this.dtpPurchaseDate.DataBindings.Add("Value", this.bindingSource1, "PurchaseDate");
当数据库中PurchaseDate字段的值为Null时.此控件可以显示为空.值为日期时.此控件也可以正确显示. this.dtpPurchaseDate = new WinUI.MDateTimePicker();
DataSet ds=new PurchaseMainBLL().GetAllList();
this.bindingSource1.DataSource = ds;
this.bindingSource1.DataMember = ds.Tables[0].TableName;
this.dtpPurchaseDate.DataBindings.Add("Value", this.bindingSource1, "PurchaseDate");
只是当我按DELETE时,控件显示的日期被删除.Value也变成了null.但与它绑定的PurchaseDate的值不会改变.
请各位牛人.帮忙看一下.谢谢!
---------------------------------------------------------------------
每个人都是一座山.世上最难攀越的山,其实是自己.往上走,即便一小步,也有新高度.
每个人都是一座山.世上最难攀越的山,其实是自己.往上走,即便一小步,也有新高度.
--做最好的自己,我能!!!