Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# WinForm
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 1:59am
Comments
RDBurmon at 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
0 OriginalGriff 210
1 Richard MacCutchan 100
2 kbrandwijk 90
3 ProgramFOX 80
4 Mukesh Bhagat 65
0 Sergey Alexandrovich Kryukov 9,050
1 OriginalGriff 8,151
2 CPallini 2,613
3 Richard MacCutchan 2,221
4 Abhinav S 1,928


Advertise | Privacy | Mobile
Web03 | 2.8.140827.1 | Last Updated 13 Jun 2012
Copyright © CodeProject, 1999-2014
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