空值设置为Datetimepicker [英] Null value set to Datetimepicker

查看:86
本文介绍了空值设置为Datetimepicker的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

public partial class mst_item : Form
    {
        SqlConnection cn = null;
        int originalExStyle = -1;
        bool enableFormLevelDoubleBuffering = true;
        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)
            {
                dtBirthdayNullable  = (b.Control as DateTimePicker);

                if (dtBirthdayNullable != null)
                {
                    if (e.Value == null)
                    {
                        dtBirthdayNullable.ShowCheckBox = true;
                        dtBirthdayNullable.Checked = false;
                        e.Value = dtBirthdayNullable.Value;
                    }

                    else
                    {
                        dtBirthdayNullable.ShowCheckBox = true;
                        dtBirthdayNullable.Checked = true;
                        // leave e.Value unchanged – it’s not null, so the DTP is fine with it. 
                    }
                }
            }
        }
        //Binding v    = new Binding("Value");
        //Binding b = new Binding( "Value", person, "BdayNullable", true ); 
        // dtBirthdayNullable.DataBindings.Add( b );  
        //  b.Format += new ConvertEventHandler( dtBirthdayNullable_Format ); 
    
        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)
            {
                dtBirthdayNullable = (b.Control as DateTimePicker);

                if (dtBirthdayNullable != null)
                {
                    if (dtBirthdayNullable.Checked == false)
                    {
                        dtBirthdayNullable.ShowCheckBox = true;
                        dtBirthdayNullable.Checked = false;
                        System.Nullable(e.Value);
                        e.Value = new Nullable() ;
                    }

                    else
                    {
                        DateTime val = Convert.ToDateTime(e.Value);
                        e.Value =  new Nullable();
                    }

                }

            }

        } 

               private void mst_item_Load(object sender, EventArgs e)
        {
            try
            {
                cn.Open();
                SqlCommand cmd = new SqlCommand("Select date1 from datetime", cn);
                SqlDataAdapter da = new SqlDataAdapter(cmd);
                DataSet ds = new DataSet();
                da.Fill(ds, "datetime");
                Binding b = new Binding("Value", ds, "datetime.date1", true);
                dtBirthdayNullable.DataBindings.Add(b);
                b.Format += new ConvertEventHandler(dtBirthdayNullable_Format);
                b.Parse += new ConvertEventHandler(dtBirthdayNullable_Parse);
            }
            catch(Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }




但给出一个错误:无法创建静态类system.Nullable
的实例 任何解决方案...




But give an error : Cannot create an instance of static class system.Nullable
any solution...

推荐答案

我认为您需要以下链接中的控件:
可空的DateTimePicker [
I think you need the control in the following link :
Nullable DateTimePicker[^]


如果您不想要日期时间选择器的默认行为,我认为是你的问题.如果日期时间距datetime不到1分钟,则可以简单地忽略该日期时间.
IF you dont want the default behavior of the date time picker, as I think is your question. You could simply ignore the date time if it has a timestap less than 1 minute from datetime.now.


您可以通过C#代码进行尝试:
You can try this via C# code:
e.Value = null ;



希望对您有所帮助.



Hope it helps.


这篇关于空值设置为Datetimepicker的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆