Jump to content
Xtreme .Net Talk

ComboBox.Items.Clear() not working correctly with .SelectedIndex = -1


Recommended Posts

Posted

--Using [VS C#.NET]--

 

This is really odd, I have a Method that is made to Refresh my GUI (clear my Combobox, re-populated them, and set the indexes back to -1). How I have noticed that after using the combobox.Items.Clear() my .SelectedIndex = -1 doesn't work.

 

Given a Combobox [cb]

 

cb.Items.Clear();

foreach (DataRow dr in ds.Tables[0].Rows)

{

cb.Items.Add(diInfo);

}

cb.SelectedIndex = -1;

 

The 1st time my code runs it is perfect, however whenever I refresh (run this section again) the .Clear and .Add work (I have tested that it refreshes) however the cb.SelectedIndex = -1 does not work anymore, the last select item is still in he combobox. Any clues/ideas? Thanks,

  • *Experts*
Posted

That is weird, after clearing the items in the combobox setting the selected index to - 1 should do nothing.

 

Try removing the cb.SelectedIndex = -1;

Member, in good standing, of the elite fraternity of mentally challenged programmers.

 

Dolphins Software

Posted

1) After invoking cb.Items.Clear(), make sure cb.Items.Count is 0.

2) What's diInfo? You iterate through the rows, but add the same object reference to the ComboBox.

3) Setting SelectedIndex to -1 just deselects. It doesn't remove any items.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...