locked
Move items between two listboxes RRS feed

  • Question

  • Hello,

    I want to move data between two listboxes on a button click

    Both the listboxes are bind to iEnumerable data source coming from database.

    The button click event shows below. I can move item with drag drop but i want to do with button click


     private void btnDriverMoveLeft_Click(object sender, RoutedEventArgs e)
            {
       
                foreach (vtGroupDriverAssigned_Result item in lbDriverAvail.SelectedItems)
                {
                    lbDriverAssigned.Items.Add(item); //Error saying "collection is read only"
                }
               
            }



    Wednesday, June 22, 2011 9:25 AM

Answers

  • Wednesday, June 22, 2011 9:34 AM
  • You can implement it by getting the selected item from the source listbox and then removing it from source and adding the new item in the destination listbox collection list.

    Here is the link of moving data by using drag and drop

    http://www.codeproject.com/KB/silverlight/SL4DragDropListBox.aspx


    Wednesday, June 22, 2011 10:42 AM

All replies

  • Wednesday, June 22, 2011 9:34 AM
  • Hi,

    How did you query those data from DB? Can you repro and show us your code? Seems like your Query with Read only operation.

    Wednesday, June 22, 2011 9:35 AM
  • Well, its complicated here.

    I am using entity framwork with WCF to get data from database.

    I got result as IEnumerable<>

    Now i am binding this to listboxes. 

    but when i try to add/remove anything in listbox i am getting error "Operation not supported on read-only collection."

    I just wander, drag drop working fine on this page, then why my code throws error here?

    Any help wil be much appreciated.

    Thanks

    Wednesday, June 22, 2011 9:39 AM
  • rjacobs:

    Is there any way that i can convert IEnumerable to IList<> or ObservableCollection?

    Wednesday, June 22, 2011 9:40 AM
  • As Rjacobs post on his reply. You can use ObservableCollection<T>, Because IEmurable<T> will return read only variable can't modify.

    Wednesday, June 22, 2011 9:44 AM
  • So what you would want to do is bind your list box items source to a property of ObservableCollection<EntityResult>()

    Wednesday, June 22, 2011 10:10 AM
  • You can implement it by getting the selected item from the source listbox and then removing it from source and adding the new item in the destination listbox collection list.

    Here is the link of moving data by using drag and drop

    http://www.codeproject.com/KB/silverlight/SL4DragDropListBox.aspx


    Wednesday, June 22, 2011 10:42 AM