Adding items in Combo box

  • Thread starter Thread starter Bala Nagarajan
  • Start date Start date
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
 
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
 
My question is how do i specify the value for the item by using the
combox.items.add()?
 
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);
}
}
 
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.
 
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.
 
Back
Top