How to copy items from one DropDownList to another

How to copy elements with hard code from one drop-down list to another, keeping 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); 
+7
c # drop-down-menu
source share
8 answers

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

 drpTypesCreateEdit.Items.AddRange(drpTypes.Items.OfType<ListItem>().ToArray()); 
+28
source share

Agree with Anthony's comment above.

However, since the selected ListItems will still reference the same objects in the original DropDownList, there will be unintended side effects when changing fields / properties.

For example:

 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); drpTypes.SelectedValue = "2"; drpTypesCreateEdit.SelectedValue = "3"; 

Both drpTypes and drpTypesCreateEdit now have a SelectedValue of "3", while this is clearly not the purpose of the above code.

Creating new ListItem objects instead of simply selecting the source object will fix this:

 drpTypesCreateEdit.Items.AddRange(drpTypes.Items.Cast<ListItem>().Select(x => New ListItem(x.Text, x.SelectedValue)).ToArray(); 
+8
source share

That would be one of the easiest 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 complicated?

+3
source share

I could not figure it out in VB.NET, and it took me a while to find this answer. Duplicate DropDownListItems without Looping

 DropDownList2.DataSource = DropDownList1.Items DropDownList2.DataTextField = "Text" DropDownList2.DataValueField = "Value" DropDownList2.DataBind() 

This will make a deep copy between the two dropdownlists.

Perhaps the administrator can delete this answer and link the two questions together.

+2
source share
 This is also good for copying DropDownList items to another. 

Example: it will copy all the elements from DropDownList1 to DropDownList2.

Elements in DropDownList1:

 <asp:DropDownList ID="DropDownList1" runat="server"> <asp:ListItem Text="A" Value="1"></asp:ListItem> <asp:ListItem Text="B" Value="2"></asp:ListItem> <asp:ListItem Text="C" Value="3"></asp:ListItem> <asp:ListItem Text="D" Value="4"></asp:ListItem> <asp:ListItem Text="E" Value="5"></asp:ListItem> </asp:DropDownList> 

1 - First, copy DropDownList1 to the array.

 ListItem[] ListitemsArray = new ListItem[DropDownList1.Items.Count]; DropDownList1.Items.CopyTo(ListitemsArray, 0); 

2 - : now copy from the array to DropDownList2.

  foreach (ListItem liItems in ListitemsArray) { DropDownList2.Items.Add(liItems); } 

This works for me !!!!!!!!!!!!!

0
source share

I did it right from one drop down to another, reducing the need to copy to the array first.

  foreach(ListItem listItem in myDropDownList.Items) { myOtherDropDownList.Items.Add(listItem) } 
0
source share

I used this as

 comboBox2.Items.AddRange(comboBox3.Items.OfType<string>().ToArray()); 
0
source share

in vb i use

 Dim cb As CheckBoxList=new CheckBoxList() cb.Items.AddRange(CheckBoxList_Source.Items.Cast(Of ListItem)().Select(Function(f) New ListItem(f.Text, f.Value)).ToArray()) 

or

 Dim cb As CheckBoxList=new CheckBoxList() cb.DataSource = CheckBoxList_Source.Items cb.DataBind() 

but this binds the value to the text instead of the true value of the original elements

this is the only way i discovered cloning elements

0
source share

All Articles