Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WPF Data Virtualization and DataGrid

Tags:

c#

.net

wpf

I am using the Data Virtualization as described by Paul McClean : http://www.codeproject.com/KB/WPF/WpfDataVirtualization.aspx

It works fine with a ListView control.

but when I use it with a DataGrid control (AsyncVirtualizationCollection), it throws exceptions :

"Value cannot be null, parameter name : key"

I don't what is the cause and how to stop that from happening. I need the editing features of DataGrid control

like image 878
Attilah Avatar asked Feb 23 '23 16:02

Attilah


1 Answers

I ran into this too. It turned out the issue was this code in VirtualizingCollection (base class of AsyncVirtualizingCollection):

public T this[int index]
{
   // snip

   // defensive check in case of async load
   if (_pages[pageIndex] == null)
      return default(T);

   // snip
}

If T is a reference type, default(T) is null, and DataGrid doesn't appreciate null row objects.

To solve this, I added a public property to VirtualizingCollection to hold the default value:

public T DefaultValue = default(T);

and changed the above code to return DefaultValue instead of default(T). Then, when I construct my AsyncVirtualizingCollection, I set DefaultValue to a dummy object that is displayed while loading is in progress.

like image 71
dlf Avatar answered Mar 05 '23 20:03

dlf