title: Setting selected item in a ListBox without looping
body: <p>I have a multi-select listbox which I am binding to a DataTable. DataTable contains 2 columns description and value. </p>
  
  <p>Here's the listbox populating code:</p>
  
  <pre><code>DataTable copytable = null;
                  copytable = GlobalTable.Copy(); // GlobalTable is a DataTable
                  copytable.Rows[0][0] = "--ALL--";
                  copytable.Rows[0][1] = "--ALL--";
  
                  breakTypeList.DataSource = copytable;
                  this.breakTypeList.DisplayMember = copytable.Columns[0].ColumnName; // description
                  this.breakTypeList.ValueMember = copytable.Columns[1].ColumnName; // value
                  this.breakTypeList.SelectedIndex = -1;
  </code></pre>
  
  <p>I am setting description as the DisplayMember and value as the ValueMember of the ListBox. Now depending on what the value is passed I need to set the selected item in the ListBox.</p>
  
  <p>Here's my code:</p>
  
  <pre><code>ListBox lb = c as ListBox;
  lb.SelectedValue = valuePassedByUser;
  </code></pre>
  
  <p>which is not working. Hence I have to resort to the code below (where I loop through all the items in the list box)</p>
  
  <pre><code>for (int i = 0; i &lt; lb.Items.Count; i++)
              {
                  DataRowView dr = lb.Items[i] as DataRowView;
                  if (dr["value"].ToString() == valuePassedByUser)
                  {
                      lb.SelectedIndices.Add(i);
                      break;
                  }
              }
  </code></pre>
  
  <p>I would like to know what is missing/ erroneous in my code. Why is lb.SelectedValue = valuePassedByUser; selecting incorrect items?</p>