Click here to Skip to main content
15,077,265 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi ,
I m using VS2008 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 few itms from this listbox and add them to second one through add button. PROBLEM: the values are being retireved from database but when i click 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]);
listBox1.Items.Remove(listBox1.SelectedItem);
}

i have also tried using this:

for (int i = 0; i < from.SelectedItems.Count; i++)
{ to.Items.Add(from.SelectedItems[i].ToString()); }
foreach (var item in new ArrayList(from.SelectedItems))
{ from.Items.Remove(item); }

from and to are listbox1 and listbox2 respectively

any help will be appreciated
Posted

1 solution

I belive you wanted to add the selected item to the listbox2 and remove it from listbox1 and the following should help..


C#
try
          {
              listBox2.Items.Add(listBox1.SelectedItem);
              listBox1.Items.Remove(listBox1.SelectedItem);

          }
          catch (Exception ex)
          {
              MessageBox.Show(ex.Message);
          }
   
Comments
Bakhshi-faisal 10-Jan-17 3:35am
   
no i want to add them without selecting them

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