title: Retrieving value from combobox Winforms / Entity Framework
body: <p>I am trying to populate my combobox with a list of companies and then get value of selected company (its <code>Id</code>) into my database.</p>
  
  <p>I have this class <code>CBItem</code>:</p>
  
  <pre><code>public class CBItem
  {
      public string Name { get; set; }
      public int Value { get; set; }
  
      public override string ToString()
      {
         return Name;
      }
  }
  </code></pre>
  
  <p>I add items to combobox using this:</p>
  
  <pre><code>using (var db = new DataContext())
  {
      var list_of_companies = db.Companies;
  
      foreach (Company c in list_of_companies)
      {
          CBItem item = new CBItem();
          item.Name = c.Name;
          item.Value = c.Id;
          comboBox1.Items.Add(item);
      }
  }
  </code></pre>
  
  <p>Problem is when I want to get value of selected item, I have tried something like this:</p>
  
  <pre><code>new_person.Company.Id = (comboBox1.SelectedItem).Value;
  </code></pre>
  
  <p>Of course it doesn't work :/ Any tips?</p>