title: How to tell when nothing selected in a ComboBox
body: <p>I have a ComboBox which is being populated by a LINQ query:</p>
  
  <pre><code>var locations = 
      from loc in db.LOCATIONs
          .Where(a =&gt; a.Omit == false)
  select new
  {
      LocationID = loc.LocationID,
      Location = loc.Location1
  };
  
  cbo = this.cboAdminLocation;
  cbo.DataSource = locations;
  cbo.DisplayMember = "Location";
  cbo.ValueMember = "LocationID";
  cbo.SelectedIndex = -1;
  </code></pre>
  
  <p>I also have a routine DisplayAccounts() which lists all accounts in a DataGridView, but filtered by the value selected in the combo, if selected, or displays all accounts if not. I thought this would be quite simple, and am trying to handle it with the following statements within DisplayAccounts():</p>
  
  <pre><code>if (this.cboAdminLocation.SelectedIndex &gt; -1)
      filterAdminLocation = Convert.ToInt32(this.cboAdminLocation.SelectedValue.ToString());
  else
      filterAdminLocation = 0;
  </code></pre>
  
  <p>and at the end of the LINQ query:</p>
  
  <pre><code>if (filterAdminLocation &gt; 0)
  {
      accountData = accountData.Where(a =&gt; a.LocationID == filterAdminLocation);
  }
  </code></pre>
  
  <p>However, I am getting an error on the first loop round (i.e. before having a chance to select a value from the Combo) of "Input string was not in the correct format.", and indeed the value of the combo at this stage is displayed as <code>{ LocationID = 1, Location = "Brentwood" }</code> and the selected index is 0. If I skip over this step and test when I have selected a value it returns an integer value as expected.</p>
  
  <p>Therefore, my question is why does get past the <code>if (this.cboAdminLocation.SelectedIndex &gt; -1)</code> test when the selected index is supposedly 0, and more to the point why is the selected index 0 when I explicitly set it to -1 when populating the control?</p>
  
  <p>I appreciate this is probably very simple, but I'm fairly new to C# so you'll have to excuse my stupidity!</p>