views:

201

answers:

2

How can I copy items hardcoded from one dropdown box to another keeping the keys and values?

drpTypes.Items.Add(new ListItem("Tipos de Acções", "1"));
drpTypes.Items.Add(new ListItem("Tipos de Combustível", "2"));
drpTypes.Items.Add(new ListItem("Tipos de Condutor", "3"));

drpTypesCreateEdit.Items.AddRange(drpTypes.Items);
+3  A: 

AddRange wants an array of ListItems. you can do it like this (C# 3+).

drpTypesCreateEdit.Items.AddRange(drpTypes.Items.OfType<ListItem>().ToArray()); 
Anthony Pegram
I don't have that method in Items :(
LuRsT
What version of .NET are you using? If 3.5+, add a using statement for System.Linq. If 2.0, you could simply opt to iterate over the items in the first list and add them one by one to the second.
Anthony Pegram
It worked, thanks a lot mate :D
LuRsT
+1  A: 

This would be one of the easier ways..

drpTypes.Items.Add(new ListItem("Tipos de Acções", "1"));
drpTypes.Items.Add(new ListItem("Tipos de Combustível", "2"));
drpTypes.Items.Add(new ListItem("Tipos de Condutor", "3"));

foreach(ListItem li in drpTypes.Items)
{
    drpTypesCreateEdit.Items.Add(li);
}

Do you need something more elaborate?

Eclipsed4utoo
Thanks, but not what I was looking for
LuRsT