Combo Box, finding the right Item

  • Thread starter Thread starter Cralis
  • Start date Start date
C

Cralis

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<IManufacturer> manufacturers =
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;
}

}

}
 
u can use caching maybe for the faster access or you can use dataview
component.u can set the rowfilter property of the dataview
component...
 
Instead of this code:

int cmbIndex = 0;
foreach (IManufacturer man in cmbManufacturer.Items)
{

if (man.id == model.manufacturer)
{
break;

}
cmbIndex++;

}
cmbManufacturer.SelectedIndex = cmbIndex;

Try "cmbManufacturer.SelectedValue = model.id".

One thing to keep in mind is that the SelectedValue property doesn't work
unless you've added your data to the ComboBox by building an ArrayList and
then setting the ComboBox.DataSource property to that ArrayList. I don't
know why it doesn't work when you simply put the object into the Items
collection for the ComboBox, especially since the DisplayMember property
does work in that case. But it doesn't. :(

Sure seems like it should. I wonder if anyone else has a good explanation
for the discrepancy.

Anyway, that means that this code:
foreach (IManufacturer man in manufacturers)
{
cmbManufacturer.Items.Add(man);
}

Should read instead:

ArrayList rgman = new ArrayList();
foreach (IManufacturer man in manufacturers)
{
rgman.Add(man);
}
cmbManufacturer.DataSource = rgman;

Pete
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top