I am trying to retrieve the displayed values of all items present in a comboBox.
First case: if the comboBox has been filled using a DataSource:
comboBox.DataSource = myDataSet.Tables[0];
comboBox.DisplayMember = "value";
comboBox.ValueMember = "id";
…I use this code:
foreach (DataRowView rowView in comboBox.Items) {
String value = rowView.Row.ItemArray[1].ToString();
// 1 corresponds to the displayed members
// Do something with value
}
Second case: if the comboBox has been filled with the comboBox.Items.Add("blah blah"), I use the same code, except I have to look in the first dimension of the ItemArray:
foreach (DataRowView rowView in comboBox.Items) {
String value = rowView.Row.ItemArray[0].ToString();
// 0 corresponds to the displayed members
// Do something with value
}
Now I would like to be able to retrieve all values without knowing the scheme used to fill the comboBox. Thus, I don’t know if I have to use ItemArray[0] or ItemArray[1]. Is it possible? How could I do that?
You can try something like this: