LoveFortyDown
LoveFortyDown

Reputation: 1021

Retrieving drop down list items from list of controls

I am attempting to retrieve a ListItemCollection from a List of controls. This List contains many controls of different types - DropDownList, Label, TextBox.

I would like to retrieve all ListItem from all the DropDownList controls contained in the original List of controls.

My thought process so far has been to extract all of the DropDownList controls into a new list, and iterate though this to pull out each ListItem - however, every DropDownList control is coming up with 0 items

ControlCollection cList = pnlContent.Controls;

List<DropDownList> ddlList = new List<DropDownList>();
foreach (Control c in cList)
{
    if (c.GetType() == new DropDownList().GetType())
    {
        ddlList.Add((DropDownList)c);
    }
}

ListItemCollection itemCollection = new ListItemCollection();
foreach (DropDownList ddl in ddlList)
{
    foreach(ListItem li in ddl.Items)
    {
        itemCollection.Add(li);
    }
}

I'm sure this is the wrong (and massively inefficient) way of doing this. Any help would be appreciated.

Upvotes: 0

Views: 928

Answers (4)

Manu Nair
Manu Nair

Reputation: 322

        comboBox1.Items.Add(button1);
        comboBox1.Items.Add(button2);
        comboBox1.Items.Add(dateTimePicker1);
        comboBox1.Items.Add(checkBox1);

        comboBox2.Items.Add(button3);
        comboBox2.Items.Add(button4);
        comboBox2.Items.Add(dateTimePicker2);
        comboBox2.Items.Add(checkBox2);

        comboBox3.Items.Add(button5);
        comboBox3.Items.Add(dateTimePicker3);
        comboBox3.Items.Add(checkBox3);
        comboBox3.Items.Add(checkBox4);

        List<ComboBox> ddlList = new List<ComboBox>();
        foreach (Control c in panel1.Controls)
        {
            if (c is ComboBox)
            {
                ddlList.Add((ComboBox)c);
            }
        }

        List<Control> itemCollection = new List<Control>();

        foreach (ComboBox ddl in ddlList)
        {
            foreach (var li in ddl.Items)
            {
                itemCollection.Add((Control)li);
            }
        }

Upvotes: 0

Ricardo Peres
Ricardo Peres

Reputation: 14535

This will do:

public IEnumerable<ListItem> GetListItems(ControlCollection controls)
{
    return controls.OfType<DropDownList>().SelectMany(c => c.Items.OfType<ListItem>());
}

Upvotes: 3

Jose M.
Jose M.

Reputation: 1316

Try this:

 var ddlList = cList.OfType<DropDownList>();


 ListItemCollection itemCollection = new ListItemCollection();
 // option 1
 var temp = ddlList.Select(ddl => ddl.Items.Cast<ListItem>()).SelectMany(li => li).ToArray();
 itemCollection.AddRange(temp);
 // or option 2
 var temp = ddlList.Select(ddl => ddl.Items.Cast<ListItem>()).SelectMany(li => li);
 foreach (var listItem in temp)
 {
     itemCollection.Add(listItem);
 }

Upvotes: 0

Sander
Sander

Reputation: 1314

I currently do not have an installation where I can test this, but as a line of thought I would use Linq to do this.

Here is an example that you should be able to use;

var type = new DropDownList().GetType();

var listOfControl = from c in pnlContent.Controls
                    where c.GetType() == type
                    select ((DropDownList)c).Items;

Upvotes: 0

Related Questions