Adding and removing items of databound listbox
I have a window forms application with two listboxes and 2 buttons.
lbxAllItems, on the Right, is bound to a datatable (from a dataset from the database).
When the user selects and item in lbxAllItems and clicks on btnAdd, I would like to remove that item from lbxAllItems and add it to lbxSelection.
Conversely, when the user selects an item in lbxSelection and clicks on btnRemove, I would like to remove that item from lbxSelection and add it to lbxAllItems.
This is what I've tried and its not working:
private void btnAdd_Click(object sender, System.EventArgs e)
{
lbxSelection.Items.Add(((DataRowView)lbxAllItems.SelectedItem)["Items"]).ToString();
lbxAllItems.Items.Remove(((DataRowView)lbxAllItems.SelectedItem)["Items"]);
}
private void btnRemove_Click(object sender, System.EventArgs e)
{
lbxAllItems.Items.Add(lbxSelection.SelectedItem.ToString());
lbxSelection.Items.Remove(lbxSelection.SelectedItem.ToString());
}
However, this is not working. When I clicking btnAdd, an item is added, but the item from the lbxAllItems is not removed.
And when I click on btnRemove, the item IS removed from lbxSelection, but not added to the lbxAllItems.
Is this an issue because lbxAllItems is bound?
Thanks.