title: Dynamic combobox list databinding in C# WinForms
body: <p>Have read a lot this resource and really like it, but now i met problem which i can't neither resolve by myself nor find similar solution.
  I'm using C# winforms and linqtosql.
  In my userforms I use additional view-class to bind list for comboboxes to let users be able to get and use a name-list of objects while being forbidden to get a whole object itself. (This is not question whether it is a good practice, no matter.)</p>
  
  <p>For example(this is not real code, just for look):
  ORM classes:</p>
  
  <pre><code>public class Contract
  {
      public string ID { get; set; }
      public string Name { get; set; }
      public Contractor Contractor { get; set; }
      public string ContractorID { get; set; }
  }
  public class Contractor
  {
      public string ID { get; set; }
      public string Name { get; set; }
      public string Phone { get; set; }
  }
  </code></pre>
  
  <p>This is additional view-class which is mapping for sqlserver view <code>Contractor_List</code> <code>(SELECT c.ID, c.Name FROM Contractors c)</code></p>
  
  <pre><code>public class Contractor_List
  {
      public string ID { get; set; }
      public string Name { get; set; }
  }
  </code></pre>
  
  <p>UserForm:</p>
  
  <pre><code>public class ContractForm : Form
  {
      void Init()
      {
          TextBox nameBox = new TextBox();
          ComboBox contractorBox = new ComboBox();
          BindingSource contractSource = new BindingSource();
          contractSource.DataSource = typeof (Contract);
  
          nameBox.DataBindings.Add("Text", contractSource, "Name", false, DataSourceUpdateMode.OnValidation);
          contractorBox.DataBindings.Add("SelectedValue", contractSource, "ContractorID", false, DataSourceUpdateMode.OnValidation);
  
  
          BindingSource contractorListSource = new BindingSource();
          contractorListSource.DataSource = typeof (Contractor_List);
  
          contractorBox.DisplayMember = "Name";
          contractorBox.ValueMember = "ID";
      }
  
  }
  </code></pre>
  
  <p>OK. 
  My idea is to load contractorBox.DataSource (it’s binding source) when contractorBox.SelectedValue is set.
  I found that SelectedValue is not overridable, so I decided to inherit combobox and to create in it a new property called “ID” and do following stuff instead 
  In form:</p>
  
  <pre><code>contractorBox.DataBindings.Add("ID", contractSource, "ContractorID");
  </code></pre>
  
  <p>In Control (this is real code):</p>
  
  <pre><code>    object _id;
      bool _listInitialized;
      public object ID
              {
                  get
                  {
                      return _id;
  
                  }
                  set
                  {
                      if (!_listInitialized)
                      {
                          var bindingSource = DataSource as BindingSource;
                          if (bindingSource != null)
                          {
                              var t = (bindingSource.DataSource as Type);
                              var rst = … //Getting List
                              if (rst!=null)
                              {
                                  bindingSource.DataSource = rst;
                                  _listInitialized = true;
                                  SelectedValueChanged += delegate {
                                      if (SelectedValue != ID)
                                      {
                                          ID = SelectedValue;
                                      }
                                  };
                              }
                          }
                      }
                      else
                      {
                          _id = value;
                          if (SelectedValue != ID)
                          {
                              SelectedValue = value;
                          }
                      }
  
                  }
              }
  </code></pre>
  
  <p>So, this code works fine. I can load form, Contract object and list of contractors and get right contractor name in combobox.
  But. I have problem with backing Binding of “ID” property. When contractor in combobox is changed Contract object doesn’t update (neither <code>ContractorID</code>, no  <code>Contractor</code> Itself) while <code>ID</code>, <code>SelectedValue</code> and <code>SelectedItem</code> of combobox change properly.
  Why? What have I do to make this working.</p>