Data Binding – What are NullValue and DataSourceNullValue properties?

Are you confused about what is NullValue and DataSourceNullValue properties on the Binding class? Are you confused about how Binding behaves when formatting or parsing when either of these is set? If you answered YES to any of these above then read on for a simple explanation of what these properties are and how they can be used!


The Binding class exposes 2 properties: NullValue and DataSourceNullValue. Let’s look at each of these:


1)      NullValue simply specifies what a control should display when it needs to display a null value. E.g. Say you have Textbox control that is bound to a column of a DataTable. Consider that some values in the DataTable are DBNull.Value. In this case, if you would like DBNull.Value to be displayed by the TextBox as say, “(null)” then you would achieve this by setting NullValue property of the binding to “(null)”.


Note: TextBox will display the NullValue if following conditions are met:

a)      Value in the DataTable is either DBNull.Value, null or DataSourceNullValue (read more about this below) AND

b)      Formatting is enabled.


Here is a code snippet:


            DataTable dt = new DataTable();





            dt.Rows.Add(new object[] { null });



            Binding b = new Binding("Text", dt, "Name", true);

            b.NullValue = "nv";



TextBox now displays “nv” for all rows in the DataTable except the first two rows.



2)      DataSourceNullValue specifies what value of the data source is considered a null value and also specifies what value should be written back to the data source for a null value.

e.g. Say just like above you have the DataTable. Say you want to consider string “dsnv” as a null value. To do so you would set DataSourceNullValue property on the Binding to “dsnv”. If you set this property to “dsnv”, then "dsnv" is considered as a null value and the TextBox displays this value as value specified by the NullValue property.


Here is a code snippet:


            Binding b = new Binding("Text", dt, "Name", true);

            b.NullValue = "nv";

b.DataSourceNullValue = "dsnv";



In this case, note that all rows except only the first one are shown as “nv”.


Also, if you set the Text of the Textbox to any of the “logical nulls” (i.e. to null or NullValue) then it would get stored back in the data source as DataSourceNullValue. Say for example, if the TextBox is showing the first row i.e. showing the text “foo”. Now if you type in NullValue i.e. string “nv” then it would be stored back in the data table as string “dsnv” i.e. as DataSourceNullValue.



These 2 properties are very powerful and make formatting and parsing null values extremely simple.


As always, let me know if you have any questions. Happy Binding!

Comments (3)

  1. Lars Blog... says:

    Ich habe die letzten beiden Tage damit zugebracht, eine eigentlich kleine Geschichte unter .NET 2.0 zu realisieren.Folgendes Szenario:Auf einer Form gibt es eine ComboBox. Diese ist an ein BusinessObject gebunden (z. B. eine Person). Genauer ist die Co

Skip to main content