DataBinding a DateTimePicker raises “DataBinding cannot find a row in the list that is suitable for all bindings.”

流过昼夜 提交于 2019-12-07 12:03:17

问题


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.

来源:https://stackoverflow.com/questions/12859973/databinding-a-datetimepicker-raises-databinding-cannot-find-a-row-in-the-list-t

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!