How to...

  • Thread starter Thread starter Jacek Jurkowski
  • Start date Start date
J

Jacek Jurkowski

.... bind nullable control property to nullable value?
The simpliest scenario:

public class a
{
Nullable<DateTime> myDate = null;

public Nullable<DateTime> MyDate
{
get{return myDate;}
set{myDate = value;}
}
}

public class MyControl : UserControl
{
Nullable<DateTime> date = null;

public Nullable<DateTime> Date
{
get{return date;}
set{date = value;}
}
}

public class MyForm : Form
{
a class_a = new a();
MyControl my_control = new MyControl();

protected override void OnLoad(EventArgs e)
{
this.Controls.Add(my_control);
// Next line causes the error.
// "Cannot format the value to the desired type".
my_control.DataBindings.Add("Date",class_a,"MyDate");
}
}

What to do to allow such binding in the simpliest way?
 
... bind nullable control property to nullable value?
The simpliest scenario:

public class a
{
    Nullable<DateTime> myDate = null;

    public Nullable<DateTime> MyDate
    {
        get{return myDate;}
        set{myDate = value;}
    }

}

public class MyControl : UserControl
{
    Nullable<DateTime> date = null;

    public Nullable<DateTime> Date
    {
        get{return date;}
        set{date = value;}
    }

}

public class MyForm : Form
{
        a class_a = new a();
        MyControl my_control = new MyControl();

        protected override void OnLoad(EventArgs e)
        {
            this.Controls.Add(my_control);
            // Next line causes the error.
            // "Cannot format the value to the desired type".
            my_control.DataBindings.Add("Date",class_a,"MyDate");
        }

}

What to do to allow such binding in the simpliest way?

I'm not 100% sure that it is possible, but try playing with
Binding.NullValue and Binding.DataSourceNullValue (the latter in
particular defaults to DBNull.Value rather than null, which may be the
cause of your troubles).
 
Back
Top