Click here to Skip to main content
12,693,704 members (30,756 online)
Rate this:
 
Please Sign up or sign in to vote.
See more: C#
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:
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 10-May-12 2:59am
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.
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

Do not use .ToString() on items. Try something like this:
    for (int i = 0; i < listBox1.Items.Count; i++)
    {
        listBox2.Items.Add(listBox1.Items[i]);
    }
    listBox1.Items.Clear();
  Permalink  
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 2

This should wotk:
for (int i = 0; i < listBox1.SelectedItems.Count; i++)
  {
      listBox2.Items.Add((ListViewItem)listBox1.SelectedItems[i].Clone());
      listBox1.Items.Remove(listBox1.SelectedItems[i]);
  }
  Permalink  
v2
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 3

for (int i = 0; i < listBox1.Items.Count; i++)
   {
       listBox2.Items.Add(listBox1.Items[i]);
   }
   listBox1.Items.Clear();
  Permalink  

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

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy | Mobile
Web02 | 2.8.170118.1 | Last Updated 13 Jun 2012
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100