Click here to Skip to main content
15,893,486 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
See more:
C#
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);
            }
        }



But give an error : Cannot create an instance of static class system.Nullable
any solution...
Posted
Updated 1-Aug-12 8:36am
v3
Comments
[no name] 1-Aug-12 14:28pm    
Excatly what it says... you cannot instantiate an instance of Nullable.
2pai 1-Aug-12 14:32pm    
"Cannot create an instance of static class Sstem.Nullabe"
[no name] 1-Aug-12 14:33pm    
And?
2pai 1-Aug-12 14:36pm    
only this
[no name] 1-Aug-12 14:39pm    
What part of "you cannot do that" do you not understand? The error is very clear. You cannot create an instance of Nullable. You have to find some other way to do whatever it is that you are trying to do.

I think you need the control in the following link :
Nullable DateTimePicker[^]
 
Share this answer
 
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.
 
Share this answer
 
You can try this via C# code:
C#
e.Value = null ;


Hope it helps.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900