Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

c# loop through combobox where datasource is a datatable with text

Tags:

c#

.net

winforms

Is it possible to run through every item in a combobox using a foreach loop? How would I do it?

The thing is I have a System.Data.DataRowView there because the combobox is attached to a DataTable. How do I convert from that to string?

like image 453
Alex Gordon Avatar asked Nov 08 '10 22:11

Alex Gordon


1 Answers

Generally, it looks like this:

foreach(object item in myComboBox.Items)
{
   DataRowView row = item as DataRowView;

   if(row != null)
   {
        string displayValue = row["myColumnName"].ToString();

        // do something
   }
   else
       // error: item was not of type DataRowView
}

also see http://msdn.microsoft.com/en-us/library/system.windows.forms.combobox.items.aspx

like image 176
Remus Avatar answered Sep 22 '22 06:09

Remus