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

后端 未结 6 1016
夕颜
夕颜 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:54

    Using Vs 2015 and having no luck binding a DateTimePicker control. The easiest thing seems to be simply not binding it- handle passing the value of your object to the control and from the control to your object manually. A few lines of code will save you lots of headaches...

    private void BindData()
    {
        // can't bind the datetimepicker, so handle it manually...
        if (o.myDate == null)
        {
            dtDatePicker.Checked = false;
        }
        else
        {
            dtDatePicker.Checked = true;
            dtDatePicker.Value = o.myDate.Value;
        }
    }
    
    private void Save()
    {
        if (dtDatePicker.Checked)
        {
            o.myDate = dtDatePicker.Value;
        }
        else
        {
            o.myDate = null;
        }
    }
    

提交回复
热议问题