none
how to copy all the listbox items to another listbox

    Question

  • hai

                       i want to copy all the list box items to another listbox where first listbox bound to database...... if any one have any idea how to do it in asp.net with c# help me..

    Monday, May 21, 2012 3:59 AM

Answers

  • Hi, 

    You can copy listbox items to as, 

    object[] objCollection = new object[listBox1.Items.Count];
    listBox1.Items.CopyTo(objCollection, 0);
    
    listBox2.Items.AddRange(objCollection);

    I hope this helps you...


    If this post answers your question, please click "Mark As Answer". If this post is helpful please click "Mark as Helpful".

    Monday, May 21, 2012 4:33 AM
  • Hi,

    Use the following code to copy all the items from ListBox1 to ListBox2.

    foreach (ListItem l in ListBox1.Items)      

      {         

       ListBox2.Items.Add(l);  

          }

    Monday, May 21, 2012 4:25 AM
  • Hi,

    You can try with the below code

        protected void SelectAll_Click(object sender, EventArgs e) 
        { 
     
            ListBox2.Items.Clear(); 
     
            for (int i = 0; i < ListBox1.Items.Count; i++) 
            { 
     
                ListItem li = new ListItem(); 
     
                li.Text = ListBox1.Items[i].Text; 
     
                li.Value = ListBox1.Items[i].Value; 
     
                ListBox2.Items.Add(li); 
     
     
            } 
     
        } 

     

    for more details Check this link for Passing Data from one List Box to another List Box


    PS.Shakeer Hussain

    Monday, May 21, 2012 5:50 AM

All replies

  • Hi,

    Use the following code to copy all the items from ListBox1 to ListBox2.

    foreach (ListItem l in ListBox1.Items)      

      {         

       ListBox2.Items.Add(l);  

          }

    Monday, May 21, 2012 4:25 AM
  • On 5/20/2012 11:59 PM, prabhakar78 wrote:
    > hai
    >
    > i want to copy all the list box items to another listbox where first
    > listbox bound to database...... if any one have any idea how to do it in
    > asp.net with c# help me..
    >
     
    Well you would create a custom object like the Person object in the link.
     
     
    You load the Person object into List<t>
     public List<Person> persons = new List<Person>();
     You make the database read and get the returned results where you will
    use a datareader to read the results.
     
    While in the datareader loop
    {
       var person = new Person();
       // populate Person from datareader
      persons.Add(Person);
     
    }
     Then you databind the control to persons.
     
    Control1.Datasource = persons;
    Control2.Datasource = persons;
     You will have to load persons at the Page_load, when it is not a
    PostBack and save persons to a session variable so you get it out of
    memory and bind it to the control.
     
    In this why, you only hit the database once, you hold the collection in
    session, and bind the collection to a control that's using the same data.
      
    Monday, May 21, 2012 4:27 AM
  • Hi, 

    You can copy listbox items to as, 

    object[] objCollection = new object[listBox1.Items.Count];
    listBox1.Items.CopyTo(objCollection, 0);
    
    listBox2.Items.AddRange(objCollection);

    I hope this helps you...


    If this post answers your question, please click "Mark As Answer". If this post is helpful please click "Mark as Helpful".

    Monday, May 21, 2012 4:33 AM
  • Hi Prabhakar,

    I think you need to copy the selected item from one listbox to another listbox when button is clicked, so i assumed like this for it in the button click event just one line of code is enough as below but you need to decorate this statement with an if condition to verify whether it is null or not.
    listBox2.Items.Add(listBox1.SelectedItem);

    Hope it will help you...


    Sai Kumar K (MCP)
    Blog: Sai's Stuff.
    WebSite: SantoshTechnologies.
    MCP ID:Microsoft Transcript.

    Monday, May 21, 2012 5:08 AM
  • Hi,

    You can try with the below code

        protected void SelectAll_Click(object sender, EventArgs e) 
        { 
     
            ListBox2.Items.Clear(); 
     
            for (int i = 0; i < ListBox1.Items.Count; i++) 
            { 
     
                ListItem li = new ListItem(); 
     
                li.Text = ListBox1.Items[i].Text; 
     
                li.Value = ListBox1.Items[i].Value; 
     
                ListBox2.Items.Add(li); 
     
     
            } 
     
        } 

     

    for more details Check this link for Passing Data from one List Box to another List Box


    PS.Shakeer Hussain

    Monday, May 21, 2012 5:50 AM
  • the fastest copy from list to List i have seen !!!. Thanks


    • Edited by philipds431 Tuesday, August 27, 2013 7:01 PM
    Tuesday, August 27, 2013 7:00 PM