WPF DataGrid – Binding with Nullable Types

I’ve started using the WPF DataGrid (http://www.codeplex.com/wpf) that was released as part of the WPF Toolkit. Considering its free and I don’t have to purchase a third party DataGrid, its awesome. A little problem I ran into recently was to do with an object I was using to databind to. The obect contains two ‘int?’ nullable types and as a result once you add a value into the field I was finding that any attempt to delete out the value failed.

To solve this problem I looked more into the System.Windows.Data.Binding class to see if there were any properties I could set that may help with the databind. It was at this point I found the ‘TargetNullValue’ property which just happens to do the job.

[N.B. I’m 98% sure the below explanation of my problem is correct but it is based upon my own assumptions and deductions so I’m reserving 2% for the chance its wrong]
What TargetNullValue does is allow you to declare the value to use if the source is null. The problem that I’m encountering makes sense as the DataGrid uses reflection to find the types to display and as a result it will hit a ‘Nullable’ type which provides no details on what should be shown to represent the wrapped value type. In my example I was using Nullable<int> meaning that the WPF DataGrid would see that my object had a null value but as it’s type is ‘Nullable<>’ it has no clue what to show for that null value. By using TargetNullValue I can specify that for a null value from the source the DataGrid should use ‘string.Empty’ as the value to display.

Below I’ve pasted an example of what I declared for my column in XAML and how to declare the binding.

<Window x:Class="DataGridDemo.DataGridDemoWindow"
    Title="Data Grid Demo" Height="533" Width="790">
  <my:DataGrid Name="uxDataGrid" xmlns:my="http://schemas.microsoft.com/wpf/2008/toolkit" HeadersVisibility="Column">
      <my:DataGridTextColumn Header="Nullable Number" Binding="{Binding Path=NullableNumber, TargetNullValue={x:Static sys:String.Empty}}" />

4 thoughts on “WPF DataGrid – Binding with Nullable Types

  1. “Considering its free and I don’t have to purchase a third party DataGrid, its awesome.”

    Did you miss Xceed DataGrid for WPF Express Edition? It’s been on the market for two years now and it is free. We’re upping the ante this January and releasing a major upgrade to the free Express Edition, bringing performance up, adding a few mini-features the Microsoft grid introduced, and generally offering a much more stable, bug free, and feature-rich product than Microsoft’s WPF datagrid. Plus, you’ll have something Microsoft’s grid cannot offer: an instant upgrade path. If you ever end up needing a very advanced feature, you can purchase the Pro Edition and just turn the feature on.

    Have a great 2009.

    • When I was originally looking for the Microsoft WPF Datagrid I did come across that control but didn’t look into it. I’ll take a look at it and perhaps put a quick review on how I think it compares with the Microsoft WPF Datagrid.

  2. Chris, that sounds pretty good. Is the source code also freely available as with the WPF DataGrid? I’ve had a few cases where looking at the internals has helped me in comprehension and, obviously, it also gives a path for adding features.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s