How can I find the index of an item in a list without looping through it?
Currently this doesn\'t look very nice - searching through the list for the same item twice
That's all fine and good -- but what if you want to select an existing element as the default? In my issue there is no "--select a value--" option.
Here's my code -- you could make it into a one liner if you didn't want to check for no results I suppose...
private void LoadCombo(ComboBox cb, string itemType, string defVal = "")
{
cb.DisplayMember = "Name";
cb.ValueMember = "ItemCode";
cb.DataSource = db.Items.Where(q => q.ItemTypeId == itemType).ToList();
if (!string.IsNullOrEmpty(defVal))
{
var i = ((List)cb.DataSource).FindIndex(q => q.ItemCode == defVal);
if (i>=0) cb.SelectedIndex = i;
}
}