how to set datetimepicker with null value if date not selected(c# winforms)

后端 未结 6 1027
夕颜
夕颜 2020-12-14 03:21
Binding b = new Binding( \"Value\", person, \"BdayNullable\", true );
dtBirthdayNullable.DataBindings.Add( b );
b.Format += new ConvertEventHandler( dtBirthdayNullab         


        
6条回答
  •  半阙折子戏
    2020-12-14 03:55

    DateTimePickers can't be set to null because DateTime can't be null, but you can set them to DateTime.MinValue which is the default value for an uninitialized DateTime. And then you just check if the dtp.Value = DateTime.MinValue and if so, treat it as null.

    However, if you want to really distinguish when no value has been selected, the easiest way is to set DateTimePicker.ShowCheckBox to true, and then you check dtp.Checked and if it's true, you read the value, otherwise you treat it as a null.

提交回复
热议问题