0

I have this form right here, where the user will add the start date of the diagnosis. pretty simple right? but in the end diagnosis how would I set it to a blank? or 0 value? here's my form.enter image description here

user962206
  • 15,637
  • 61
  • 177
  • 270
  • Could you show the code? Hint though would be to use Nullable DateTime? so it can store null. DateTime is a value object and therefore cannot store null – James Kyburz Mar 25 '12 at 14:21
  • In my database, the data type is only date. I mean in the UI how would set their initial values to null or 0? it will have a value if the user clicks it. – user962206 Mar 25 '12 at 14:23
  • possible duplicate of [DateTimePicker Null Value (.NET)](http://stackoverflow.com/questions/284364/datetimepicker-null-value-net) – bluish Nov 13 '13 at 08:38

7 Answers7

4

The best you can do is to set ShowCheckBox to true. If the CheckBox is not checked, consider that no end date is set.

Thomas Levesque
  • 286,951
  • 70
  • 623
  • 758
2

i think, it is not possible to set it as blank or 0 value.. the least you can do is to set it in its MinDate if it's null..

this one works for me..

 dateTimePicker1.Format = DateTimePickerFormat.Custom;

 dateTimePicker1.CustomFormat = " ";
Qantas 94 Heavy
  • 15,750
  • 31
  • 68
  • 83
rutchel
  • 21
  • 1
1

you don't want it to be set to 0. It makes no sense (1/1/01 ? most of DB doesn't event store this date)

You should put it either the same as start or in a reasonable margin which describe the common scenario (like + 2 weeks)

Dani
  • 14,639
  • 11
  • 62
  • 110
0

try to use

dateTimePicker1.Value.Date

It would return you only date

Mujassir Nasir
  • 1,640
  • 4
  • 31
  • 52
  • 1
    This does not provide an answer to the question. To critique or request clarification from an author, leave a comment below their post. – Qantas 94 Heavy Nov 24 '13 at 00:13
0

All solved now just had to adjust. As what you guys were saying i wasn't setting the value or the properties so it would all ways be null anyway

private string _BirthDate;

        public Form1()
        {
            InitializeComponent();
            dateTimePicker1.MaxDate = DateTime.Now; // Ensures no future dates are set.
            dateTimePicker1.Format = DateTimePickerFormat.Custom;
            //dateTimePicker1.Checked = true;
            dateTimePicker1.CustomFormat = dateTimePicker1.CustomFormat;
            dateTimePicker1.Value = DateTime.Now;
        }
        public string BirthDate
        {
            set
            {
                if (dateTimePicker1.Value.Date == DateTime.Today)
                {
                    dateTimePicker1.CustomFormat = " ";

                }
                else
                {
                    _BirthDate = value;
                }
            }
            get { return _BirthDate; }
        }


        private void button1_Click(object sender, EventArgs e)
        {

           BirthDate = dateTimePicker1.Value.ToString();
        }
Chaos
  • 633
  • 1
  • 6
  • 14
0

Try setting the Value property. However I think there is a limit on how far back you can set the value.

Devin M
  • 9,636
  • 2
  • 33
  • 46
  • I want the datetime picker to display blank. or a null value how can I achieve that? – user962206 Mar 25 '12 at 14:27
  • You cant null the date value you could only set it to a date far in the past. See Thomas Levesque's answer about placing a check box next to the date, that might get you what you want. – Devin M Mar 25 '12 at 14:29
0

You can put an external checkbox and disable the datetimepicker when you wish to signify no-date.

Alex
  • 768
  • 1
  • 5
  • 13