Archive for September, 2009

Save / manage column layouts / sortings in WPF Data Grid

September 25, 2009

I present here a solution demonstrating Advanced Data Grid: a user control allowing you to save / retrieve / manage column layouts and sortings you have done in WPF Data Grid from WPFToolkit (see at wpf.codeplex.com). Due to security rules of wordpress.com, it cannot be uploaded as zip file, please firstly remove the .doc extension of file  Advanced Data Grid for WPF. Solution is developed under VS 2008.

It is not Open Source, you are not allowed to use the source code.
Description of code see in: CodeBeispiel (in German!), using manual here: developer manual for Advanced Data Grid (in English).

LINQ query: NullReferenceException

September 23, 2009

Code example:

1    Dim queryResult = customers.Where(Function (cu) cu.Type.id=myId)
2    If queryResult.Count>0 Then
3        End Sub
4    End If
5    …something

The line 1 runs OK, the lne 2 calls NullReferenceException!
You can think, because queryResult is empty? False!
queryResult was evaluated only at line 2, as the results are used.
It is “Deferred Evaluation” of LINQ.
And during the evaluation a Null Reference was found. For example, customer.Type is Nothing!

Think of it: the LINQ query will be evaluated, if it will be used, not before!

If you use the results in function .ToList():

1    Dim queryResult as List(Of customer) = customers.Where(Function (cu) cu.Type.id=myId).ToList()

then NullReferenceException will be called immediately in line 1, because the LINQ will be evaluated in the same code line, but only for function .ToList(), not in the LINQ expression itself!