Click here to Skip to main content
15,895,423 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,
I want to remove the selected item from the combobox. The combobox is
bound to database.
I used code like this
C#
foreach (int selectedindex in combobox2.SelectedIndex.ToString())
{
  combobox2.Items.RemoveAt(selectedindex);
}

but it will return an error you can't set the property for item collection

if anyone has an idea about this help me with example.
Posted
Updated 1-Apr-12 23:02pm
v2

Um.
Where do I start?

Why are you calling ToString on an int in order to use it as the Collection in a foreach?
Why are you trying to use a foreach anyway?

When you do
C#
int x = 34;
foreach (int i in x.ToString())
The value of "i" inside the loop will be the value of each individual character in the string form of the integer x: in this case the loop will go twice, with values i == 51 and i == 52 (51 is the character '3' and 52 is the character '4')

So looping on these and trying to delete them is frankly really, really, stupid. If it worked, it would delete two items almost randomly from the combobox.

What are you trying to do, that you think this is a good solution?
 
Share this answer
 
Comments
Charlie Denvir 8-Mar-23 8:59am    
lads calm her down..... no need for the violence
OriginalGriff 8-Mar-23 9:02am    
I think after 12 years, we're pretty chill ...
Dear Praveen,


if you try with indexes it won't work correctly.
While removing items from list Indexes will change automatically.


comboBox2.Items.Remove(comboBox2.SelectedItem);

This links may help you

http://msdn.microsoft.com/en-us/library/system.windows.forms.combobox.selectedindexchanged.aspx
http://www.mycsharpcorner.com/Post.aspx?postID=52[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900