I m using VS2010 with SQL server 2008 at the backend. I m creating a windows application in which I have 2 listboxes.the data is being retrieved from SQL server database in listbox1.i want to select all itms from this listbox1 and add them to listbox2. And remove itms from this listbox2 and add them to listbox1.

Recommended Answers

All 3 Replies

listBox1.Items.Clear(); // for removing all items



   foreach(ListItem listItem in listBox1.Items) /* adding items to another listbox*/
    {
       if (listItem.Selected == True)
       {
          listBox2.Items.Add(listItem);
       }
    }

What would be wrong, to first copy Listbox2 to Listbox1 and then fetch data from DB to Listbox2?

//adding all items from listbox1 to listbox2
            foreach (string temp in listBox1.Items)
                listBox2.Items.Add(temp);

//after you removed data, the below code will help you to regenarate the listbox1
            listBox1.Items.Clear();
            foreach (string temp in listBox2.Items)
                listBox1.Items.Add(temp);
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.