add combobox items with different text and value
September 3, 2009
We can do it with DataBinding a combobox with DataSet or DataTable object. And we can also do it with customer objects. There are two ways to do this.
Here is the customer object:
public class cbxItem
{
private string _Text;
private long _Value;
public string Text{get{return _Text;}}
public long Value{get{return _Value;}}
public cbxItem(string theText, long theValue)
{
_Text = theText;
_Value = theValue;
}
}
Way 1: using DataBinding with customer objects as DataSet
……
ArrayList cbxObjects= new ArrayList();
while(theDataReader.Read())
{
cbxObjects.Add (new cbxItem (theDataReader.GetString(1),theDataReader.GetInt32 (0)));
}
this.ComboBox1.DataSource = Authors;
this.ComboBox1.DisplayMember =”Text”;
this.ComboBox1.ValueMember = “Value”;
……
Way 2: directly using ComboBox.items.Add(obj) with customer objects:
……
while(theDataReader.Read())
{
ComboBox1.Items.Add (new cbxItem (theDataReader.GetString(1),theDataReader.GetInt32 (0)));
}
……
