zoukankan
html css js c++ java
自己写的一个用于解决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;
}
}
}
我的问题是:
我在表单中.是这样写的
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时.此控件可以显示为空.值为日期时.此控件也可以正确显示.
只是当我按DELETE时,控件显示的日期被删除.Value也变成了null.但与它绑定的PurchaseDate的值不会改变.
请各位牛人.帮忙看一下.谢谢!
---------------------------------------------------------------------
每个人都是一座山.世上最难攀越的山,其实是自己.往上走,即便一小步,也有新高度
.
--做最好的自己,我能!!!
查看全文
相关阅读:
Win10安装组策略功能
IIS 站点批量迁移
MongoDB 异常记录
微信Web开发者工具
NHibernate 异常
Git 常用命令
IIS 反向代理
CSS应用
Oracle
Android之ActionBar学习
原文地址:https://www.cnblogs.com/tonyepaper/p/1176392.html
最新文章
找水王
梦断代码阅读笔记(3)
博客园用户体验
每日Scrum--No.9
每日Scrum--No.8
梦断代码阅读笔记(2)
每日Scrum--No.7
搜狗输入法--用户体验
每日Scrum--No.6
利用Surprise包进行电影推荐
热门文章
README.md的编写
github使用指南
Dreamweaver基本操作
Dreamweaver安装与破解
python画图
用户相似度计算
pandas中DataFrame相关
pandas读写excel
sklearn中的cross_val_score()函数
Html IE CheckBox双击问题
Copyright © 2011-2022 走看看