Loading data in background for DataGrid

Jun 1, 2009 at 11:06 AM

Hi,

 

I use the WPF DataGrid bound to a List<List<double>> as ItemsSource. At some points in my program there will be new data loaded and since this needs a few seconds in time I do it with a BackgroundWorker. When I load the data without the BackgroundWorker all works fine (well except the GUI does not respond for a few seconds), but when it is done in background i get a NullReference expection in the DataGridCellItemAutomationPeer class in the OwningCell get-accessor.

 

Here is my code used to load the data:

 

BackgroundWorker bgw = new BackgroundWorker();
bgw.WorkerReportsProgress = true;
bgw.DoWork += (o, ev) =>
                {
                    bgw.ReportProgress(100, LoadData());//Load Data creates the data
                };

bgw.ProgressChanged += (o, ev) =>

               {

                   //setting the data in progress changed so that I'm in the GUI thread again

                   Data = ev.UserState as List<List<double>>;

                   FirePropertyChanged("Data");
               };
bgw.RunWorkerAsync();

 

And that is the part where the exception is thrown because this.OwningDataGrid is null.

 

// This may be null if the cell is virtualized
        private DataGridCell OwningCell
        {
            get
            {
                return this.OwningDataGrid.TryFindCell(_item, _column);
            }
        }

 

And I can't figure out this behaviour and what I need to change so it will work.

thank you for your help

 

ernii