Results 1 to 4 of 4

Thread: [RESOLVED] [02/03] copy from one dropdownlist to another

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Jan 2006
    Posts
    1,875

    Resolved [RESOLVED] [02/03] copy from one dropdownlist to another

    I want to copy data from one dropdown list data to another one,Is it possible to do without loop?
    This is how i am doing currently

    Code:
       
               ddlSerchIn.Items.Insert(0, "Data1")
                ddlSerchIn.Items(0).Value = "Data1"
    
                ddlSerchIn.Items.Insert(1, "Data2")
                ddlSerchIn.Items(1).Value = "Data2"
    
    
               Dim lstItems As ListItem
                For Each lstItems In ddlSerchIn.Items
                    ddlAdvSearch1.Items.Add(lstItems)
                Next
    __________________
    Rate the posts that helped you

  2. #2
    Smitten by reality Harsh Gupta's Avatar
    Join Date
    Feb 2005
    Posts
    2,938

    Re: [02/03] copy from one dropdownlist to another

    Check .Items.CopyTo function of DDL class. It returns an array of ListItem.
    VB Code:
    1. Dim lstItems As New ListItem[ddlSerchIn.Items.Count]
    2. ddlSerchIn.Items.CopyTo(lstItems, 0)
    3.  
    4. ddlAdvSearch1.DataSource = lstItems
    5. ddlAdvSearch1.DataBind()
    Just wrote from top of my head.

    Edit: changing language from C# to VB
    Show Appreciation. Rate Posts.

  3. #3
    Smitten by reality Harsh Gupta's Avatar
    Join Date
    Feb 2005
    Posts
    2,938

    Re: [02/03] copy from one dropdownlist to another

    One more point to be made:

    If the first DDL is bound to a datasource, you can just retrieve its datasource, pass it to the other one and call DataBind() of second DDL.
    Show Appreciation. Rate Posts.

  4. #4

    Thread Starter
    Frenzied Member
    Join Date
    Jan 2006
    Posts
    1,875

    Re: [02/03] copy from one dropdownlist to another

    Thanks for the code snnipet,here is vb ver of same...

    Code:
     Dim lstItems As ListItem() = New ListItem(ddlSerchIn.Items.Count -1){}
     ddlSerchIn.Items.CopyTo(lstItems, 0)
     ddlAdvSearch1.AddRange(lstItems )
    __________________
    Rate the posts that helped you

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width