Click here to Skip to main content
15,868,340 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have 2 listboxes in my application. The data is being retrieved from a SQL server database. In listbox1, I want to select few items from this listbox and add them to second one through add button.

PROBLEM: The values are being retrieved from the database but when I click the add button after selecting a value from listbox1, System.Data.DataRowView is displayed in listbox2 automatically.

Listbox2 is not currently connected to any databse.

Here is the code:
C#
for (int i = 0; i < listBox1.Items.Count; i++)
               {
                   listBox2.Items.Add(listBox1.Items[i].ToString());
                   listBox1.Items.Remove(listBox1.Items[i].ToString());
               }

if anyone have any idea how to do it help me
Posted
Comments
RDBurmon 13-Jun-12 9:28am    
Thanks Everyone who replied to this thread , So Praveen, I think you have got enough responses and you should be able to mark it as your answer and close the thread. Please do so.

Do not use .ToString() on items. Try something like this:
C#
for (int i = 0; i < listBox1.Items.Count; i++)
{
    listBox2.Items.Add(listBox1.Items[i]);
}
listBox1.Items.Clear();
 
Share this answer
 
This should wotk:
C#
for (int i = 0; i < listBox1.SelectedItems.Count; i++)
  {
      listBox2.Items.Add((ListViewItem)listBox1.SelectedItems[i].Clone());
      listBox1.Items.Remove(listBox1.SelectedItems[i]);
  }
 
Share this answer
 
v2
C#
for (int i = 0; i < listBox1.Items.Count; i++)
   {
       listBox2.Items.Add(listBox1.Items[i]);
   }
   listBox1.Items.Clear();
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900