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 8373291
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T14:38:30+00:00 2026-06-09T14:38:30+00:00

I have a DataGrid in WPF (a class that extends DataGrid), and I would

  • 0

I have a DataGrid in WPF (a class that extends DataGrid), and I would like to edit the items in it. But of course I am getting the following error:

Operation is not valid while ItemsSource is in use. 
Access and modify elements with ItemsControl.ItemsSource instead.

I have tried changing the itemsSource of the DataGrid, and then adding the items, but I still get the same error. Something like:

public class MyDG:DataGrid{

    public void add(){
        List<TimesheetRecord> records = new List<TimesheetRecord>();

        foreach(TimesheetRecord rec in this.Items){
            records.Add(rec);
        }

        //DO SOME STUFF, ADD MORE ITEMS TO records

        ItemCollection col = this.Items;
        this.ItemsSource = records;
        col.Clear();

       foreach(TimesheetRecord rec in records){
            col.add(red);//exception thrown here
        }

        this.ItemsSource = col;
    }

}

I don’t understand why I am getting that error, when I have already changed the itemsSource to a different list…?

I can’t (easily) add the items to the list which is originally bound as the itemsSource, because that list exists in a different class. Would it be best for me to just have a global variable in the MyDG class that is List<TimesheetRecord> myItems = new List<TimesheetRecord>(); and then in the constructor for MyDG go this.ItemsSource = myItems

Or do you have any other suggestions how I should go about doing this? I am open to anything, as this is the first time I have used databinding, so I am probably doing something wrong…

  • 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-09T14:38:32+00:00Added an answer on June 9, 2026 at 2:38 pm

    Decalre records collection as:

    ObservableCollection<TimesheetRecord> records = new ObservableCollection<TimesheetRecord>();
    

    and keep it data-bound to the DataGrid. Manipulate records collection as needed, data binding will take care of keeping UI in sync with the collection.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a WPF DataGrid that increases in height when I add data to
I've got a WPF DataGrid that I have been using for some time, and
I'm experimenting with databinding in WPF I have this datagrid that is bound to
I have a WinDataGrid class that extends from an Infragistics UltraGrid . I have
I have an ObservableCollection of ViewModels that are sitting in a WPF DataGrid .
I would like to set up a datagrid so that whenever an item is
I have a WPF app that uses DataGrid to display some data. When I
I have a WPF user control that contains a DataGrid. This DG contains several
Problem I have a WPF Toolkit DataGrid , and I'd like to be able
I have a DataGrid (WPF 4) like this: <DataGrid Margin=0,0,0,5 VerticalAlignment=Top Height=192 BorderBrush=#aaa Background=White

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.