DataBinding a DateTimePicker raises “DataBinding c

2019-05-17 23:59发布

I have a simple test application which reproduces an error I encountered recently. Basically I have a simple WinForm with databound TextBox and DateTimePicker controls, and a button. When I execute the code below (on the button click), I get the error "DataBinding cannot find a row in the list that is suitable for all bindings". If I move the DataSource assignment into the form's constructor, I don't get the error.

If I remove the data binding for the DateTimePicker, it works fine.

Can anyone explain what the problem is ?

public partial class Form1 : Form
{
   private BindingSource bs;

   public Form1()
   {
      InitializeComponent();

      button1.Click += new EventHandler(button1_Click);

      bs = new BindingSource();
      bs.DataSource = typeof(Thing);

      this.textBox1.DataBindings.Add("Text", bs, "MyString");
      this.dateTimePicker1.DataBindings.Add(new Binding("Value", bs, "MyDate"));

      //Thing thing = new Thing { MyString = "Hello", MyNumber = 123, MyDate = DateTime.Parse("01-Jan-1970") };
      //bs.DataSource = thing;
      }

      private void button1_Click(object sender, EventArgs e)
      {
         Thing thing = new Thing { MyString = "Hello", MyNumber = 123, MyDate = DateTime.Parse("01-Jan-1970") };
         bs.DataSource = thing;
      }
   }

   public partial class Thing
   {
      public String MyString { get; set; }
      public Int32 MyNumber { get; set; }
      public DateTime MyDate { get; set; }
   }
}

Thanks

Edit:

It seems that if I change the data binding for the DateTimePicker control such that I bind to the "Text" property, the problem goes away. I don't understand why that would be though, because "Value" is valid for data binding.

0条回答
登录 后发表回答