0
votes

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]);
    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 would be appreciated.

1

1 Answers

1
votes

It would be nice if you can post the code of your add button click event.

with the first loop you copy all items from the first listbox into the second. But your description sounds as you would like to copy only the element you have selected?

I would recommend to use a checkedListBox where you can select the items and then only copy the items which are selected.

With a loop like this you can then iterate over all selected items:

foreach (string str in listB.CheckedItems)

I hope this is helpful for you. Good Luck.