I have a Combo box, populated from a List<Objects>.
The Object has a .name, and .id property. The combo box has been
populated with these using DisplayMember and ValueMember.
When I load the screen, and am editing an item, the combo must go to
the correct 'Manufacturer' that the main screen wants to see. This is
based on a Model object I have. The model has a manufacturer.
So, I populate the combo with the possible Manufacturers, but then, I
need to default the combo to the one that the main screen needs. The
code below does that, by iterating through the items in the combo.
However, I am sure there is a better way to do it. I did it the other
day in a test app, and it worked. But for the life of me, I can't get
it to work now.
The code below DOES work, but could someone tell me the easier/correct
way? I don't want to use data binding right now. I just want to move
the index to the correct item.
private void frmAddEditModel_Load(object sender, EventArgs e)
{
List<IManufacturermanufacturers =
doManufacturers.GetListOfManufacturers();
cmbManufacturer.DisplayMember = "name";
cmbManufacturer.ValueMember = "id";
foreach (IManufacturer man in manufacturers)
{
cmbManufacturer.Items.Add(man);
}
if (ModelID 0) // Are we editing?
{
IModel model = doModels.GetModel( ModelID );
ebModel.Text = model.model;
ebEdition.Text = model.edition;
int cmbIndex = 0;
foreach (IManufacturer man in cmbManufacturer.Items)
{
if (man.id == model.manufacturer)
{
break;
}
cmbIndex++;
}
cmbManufacturer.SelectedIndex = cmbIndex;
if (model.deleted == true)
{
tsStatus.Text = "Model is marked as deleted.";
}
else
{
tsStatus.Text = string.Empty;
}
}
}