Attilah
Attilah

Reputation: 17930

WPF Data Virtualization and DataGrid

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

Upvotes: 5

Views: 3826

Answers (3)

dlf
dlf

Reputation: 9373

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.

Upvotes: 6

karl
karl

Reputation: 1

I found out that this exception was actually occured in DataGridItemAttachedStorage class. Here is some callstack frames,hope someone can give a clue about it.Sorry about my poor english.

mscorlib.dll!System.Collections.Generic.Dictionary<object,System.Collections.Generic.Dictionary<System.Windows.DependencyProperty,object>>.FindEntry(object key)    未知
    mscorlib.dll!System.Collections.Generic.Dictionary<object,System.Collections.Generic.Dictionary<System.Windows.DependencyProperty,object>>.TryGetValue(object key, out System.Collections.Generic.Dictionary<System.Windows.DependencyProperty,object> value)   未知

    PresentationFramework.dll!System.Windows.Controls.DataGridItemAttachedStorage.TryGetValue(object item, System.Windows.DependencyProperty property, out object value)    未知
    PresentationFramework.dll!System.Windows.Controls.DataGridRow.RestoreAttachedItemValue(System.Windows.DependencyObject objectWithProperty, System.Windows.DependencyProperty property)  未知
    PresentationFramework.dll!System.Windows.Controls.DataGridRow.SyncProperties(bool forcePrepareCells)    未知
    PresentationFramework.dll!System.Windows.Controls.DataGridRow.PrepareRow(object item, System.Windows.Controls.DataGrid owningDataGrid)  未知
    PresentationFramework.dll!System.Windows.Controls.DataGrid.PrepareContainerForItemOverride(System.Windows.DependencyObject element, object item)    未知
    PresentationFramework.dll!System.Windows.Controls.ItemsControl.MS.Internal.Controls.IGeneratorHost.PrepareItemContainer(System.Windows.DependencyObject container, object item) 未知

Upvotes: 0

juFo
juFo

Reputation: 18567

Are you using a dictionary? debug and check if you try to add a null value as a key in the dictionary. Or check if you have a DataKeyNames parameter on a gridview with an empty key that you try to insert.

Just debug where you load/fill in the data (F10/F11). Watch your Locals window in Visual Studio.

Upvotes: 3

Related Questions