Adding items in Combo box

B

Bala Nagarajan

Hello,
Does anyone how to add items to a combo box and specify a value for
the item using combobox.items.add() method?
The reason why i am asking this is if i add items through datasource and set
the value using ValueMember property i am not able to modify the combox box
collection (Can someone elucidate why this is the case?). I want to be able
to modify the collection in the combo box and at the same time set a value
for the items. How do i acheive this?


Thanks

Bala
 
V

Vladimir.Sakharuk

as written in help:

this.comboBox1.Items.AddRange(new object[] {
"Item1",
"Item1",
"Item1"});
or
this.comboBox1.Items.Add("Item1");
this.comboBox1.Items.Add("Item2");
this.comboBox1.Items.Add("Item3");

You want to modify programmatically
then remove item add item from Items property.

If by user, you should set DropDownStyle to ComboBox not ComboBoxList
 
B

Bala Nagarajan

My question is how do i specify the value for the item by using the
combox.items.add()?
 
R

rampabbaraju

Add method takes an object as its argument. Use an object that stores a
display text and value in it. Override its ToString method so that it
returns the display value. Try the class below

public class ComboItem
{
private object bindingValue;
private object displayValue;

public object BindingValue
{
get
{
return bindingValue;
}
}

public object DisplayValue
{
get
{
return displayValue;
}
}

public ComboItem(object aBindingValue, object aDisplayValue)
{
bindingValue = aBindingValue;
displayValue = aDisplayValue;
}

public override String ToString()
{
return Convert.ToString(displayValue);
}
}
 
J

Jeffrey Tan[MSFT]

Hi Bala,

Thanks for your post.

Normally, for databinding scenario, to add a new item into ComboBox, we
should add this new item into datasource, then winform databinding will
reflect this new item into ComboBox without any problem. For example, we
can add a new DataRow into DataTable, then ComboBox will display this new
row in the list.

If I misunderstand you, please feel free to tell me, thanks

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.
 
J

Jeffrey Tan[MSFT]

Hi Bala,

Does my reply make sense to you? Is your problem resolved? Please feel free
to tell me, thanks

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.
 

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

Top