How to populate a combobox with an ArrayList containing 1 dimensional array of strings?

  • Thread starter Thread starter polaris431
  • Start date Start date
P

polaris431

All the examples I've seen showing how to populate a combobox using the
DataSource property and an ArrayList show the ArrayList object
containing objects with at least two properties. I want to create an
ArrayList that contains only rows of string data and want to assign
this to the DataSource but I can't figure out what to set the
ValueMember or DisplayMember to. Example

ArrayList myData = new ArrayList();

myData.Add("abc");
myData.Add("def");

ComboBox combo = new ComboBox();
combo.DataSource = myData;
combo.ValueMember = ???
combo.DisplayMember = ???
 
Hi,

Don't set anything. If this properties are not set the combobox will use the
object itself as display and value member. You need this properties only if
you set up the combobox with collection of complex objects - that have
properties on their own and you one use them to provide the visual and the
value.
 
All the examples I've seen showing how to populate a combobox using the
DataSource property and an ArrayList show the ArrayList object
containing objects with at least two properties. I want to create an
ArrayList that contains only rows of string data and want to assign
this to the DataSource but I can't figure out what to set the
ValueMember or DisplayMember to. Example

ArrayList myData = new ArrayList();

myData.Add("abc");
myData.Add("def");

ComboBox combo = new ComboBox();
combo.DataSource = myData;
combo.ValueMember = ???
combo.DisplayMember = ???

Do you need to use a DataSource?

Why not just use the following:

ArrayList myData = new ArrayList();
myData.Add("abc");
myData.Add("def");

foreach (string s in myData)
combo.Items.Add(s);

Bry
 
I want to avoid using a loop.

Why not just use the following:

ArrayList myData = new ArrayList();
myData.Add("abc");
myData.Add("def");

foreach (string s in myData)
combo.Items.Add(s);

Bry- Hide quoted text -- Show quoted text -
 
Back
Top