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的值不会改变.
    请各位牛人.帮忙看一下.谢谢!
  • 相关阅读:
    做报表,写Sql语句的一点收获
    ORACLE PL/SQ入门
    用Visual C#实现P2P应用程序
    css资源网站收集推荐
    Ubuntu 蓝牙全攻略
    Asp.Net细节性问题技巧精萃
    Asp.net Request获取服务器变量的方法
    如何将flash插入到Excel文件中
    XP远程桌面连接强制登录
    flash研究(三)——Falsh与JavaScript交互
  • 原文地址:https://www.cnblogs.com/tonyepaper/p/1176392.html
Copyright © 2011-2022 走看看