ComboBox - selected item not set

  • Thread starter Thread starter Backwater Geezer
  • Start date Start date
B

Backwater Geezer

More or less the code looks like this

ArrayList _contacts; // during test it has 9 objects (ClientContact)

cbxContact.DisplayMember = "Name;
cbxContact.ValueMember = "Name;

cbxContact.DataSource = _contacts;
cbxContact.SelectedItem = null;
cbxContact.SelectedIndex = -1;

public void ShowContact(int ContactId)
{
ClientContact CLCO = null;

// always use current DataSource
ArrayList data = (ArrayList)cbxContact.DataSource;

foreach (ClientContact clco in data)
{
if (clco.Id == ContactId)
{
CLCO = clco;
break;
}
}

cbxContact.SelectedItem = CLCO; // sometimes it works, sometimes not,
// drop down list has correct

if (cbxContact.SelectedItem == null)
{
cbxContact.SelectedIndex = data.IndexOf(CLCO);
// at this stage SelectedItem is no longer null and shows correct data
if (cbxContact.SelectedItem != CLCO)
MessageBox.Show("WHAT THE **** GOING ON!!!");
}

}

cbxContact has no events attached to it.

Any thoughs on this?

MH
 
Back
Top