Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9078083
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T19:31:56+00:00 2026-06-16T19:31:56+00:00

I am trying to remove selected item from one listbox and add that into

  • 0

I am trying to remove selected item from one listbox and add that into new listbox as follows code

Binding on PageLoad:

Getting the records from database into a DataTable and bind that to Listbox.

lstBox1.DataContext = dtData;

Code Bind:

  List<object> _selecteditems = new List<object>();
  foreach (var item in lstBox1.SelectedItems)
  {
      _selecteditems.Add(item);
  }
  foreach (var item in _selecteditems)
  {
      lstBox1.Items.Remove(item);
      lstBox2.Items.Add(item);
  }

Design:

<ListBox Name="lstBox1" ItemsSource="{Binding}">
  <ListBox.ItemTemplate>
    <DataTemplate>
      <TextBlock Text="{Binding ID}"/>
    </DataTemplate>
   </ListBox.ItemTemplate>
</ListBox>

<ListBox Name="lstBox2" ItemsSource="{Binding}" >
  <ListBox.ItemTemplate>
     <DataTemplate>
        <TextBlock Text="{Binding ID}"/>
      </DataTemplate>
  </ListBox.ItemTemplate>
</ListBox>

I am getting an error when removeing the item “Operation is not valid while ItemsSource is in use. Access and modify elements with ItemsControl.ItemsSource instead.”

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T19:31:57+00:00Added an answer on June 16, 2026 at 7:31 pm

    I tried by following way and its working for me.

    List<object> _selecteditems = new List<object>();
    foreach (var item in lstBox1.SelectedItems)
    {
        _selecteditems.Add(item);
    }
    foreach (var item in _selecteditems)
    {
      DataRow dr = dtSelctedDest.NewRow();
      dr[0] = ((DataRowView)item).Row[0].ToString();
      dr[1] = ((DataRowView)item).Row[1].ToString();
      dtSelctedItem.Rows.Add(dr);
      dtAllItem.Rows.Remove(dtAllItem.Rows.Remove.Select(string.Format("ID='{0}'", ((DataRowView)item).Row[0].ToString()))[0]);
     }
     lstBox1.DataContext = dtAllItem;
     lstBox2.DataContext = dtSelctedItem;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to remove an item from a listbox using C# code using
I'm trying to remove selected item from ComboBox Collection: I wrote a buttonClick: cb01.Items.Remove(cb01.SelectedItem);.
I'm trying to remove selected item from ComboBox Collection: Items are added manually, as
I'm trying to remove scrollbar from my facebook app. I tried js solution, that
I'm trying to setup my table so that the user can select one item
I am trying to delete the selected item from a ListView and the Problem
I have a horizontal nav that looks as follows: I'm trying to remove the
Ok so I'm trying to move items from one listbox to another by using
SORRY FOR THIS CONFUSION: UPDATED QUESTION: I'm trying to remove a list item from
How to remove selected items from ListBox when a datasource is assigned to it

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.