Permalink
Fetching contributors…
Cannot retrieve contributors at this time
46 lines (38 sloc) 3.26 KB
title ms.custom ms.date ms.prod ms.reviewer ms.suite ms.technology ms.tgt_pltfrm ms.topic dev_langs helpviewer_keywords ms.assetid caps.latest.revision author ms.author manager
How to: Bind Windows Forms Controls to DBNull Database Values
03/30/2017
.net-framework
dotnet-winforms
article
jsharp
BindingSource component [Windows Forms], binding to DBNull values
examples [Windows Forms], BindingSource component
controls [Windows Forms], binding to DBNull values
96494e6f-5f40-4f83-af97-bbd7192c2af8
17
dotnet-bot
dotnetcontent
wpickett

How to: Bind Windows Forms Controls to DBNull Database Values

When you bind Windows Forms controls to a data source and the data source returns a xref:System.DBNull value, you can substitute an appropriate value without handling, formatting, or parsing events. The xref:System.Windows.Forms.Binding.NullValue%2A property will convert xref:System.DBNull to a specified object when formatting or parsing the data source values.

Example

The following example demonstrates how to bind a xref:System.DBNull value in two different situations. The first demonstrates how to set the xref:System.Windows.Forms.Binding.NullValue%2A for a string property; the second demonstrates how to set the xref:System.Windows.Forms.Binding.NullValue%2A for an image property.

[!code-csharpSystem.Windows.Forms.BindingDBNull#1] [!code-vbSystem.Windows.Forms.BindingDBNull#1]

The types of the bound property and the xref:System.Windows.Forms.Binding.NullValue%2A property must be the same or an error will result, and no further xref:System.Windows.Forms.Binding.NullValue%2A values will be processed. In this situation, an exception will not be thrown.

Compiling the Code

This example requires:

  • References to the System, System.Data, System.Drawing and System.Windows.Forms assemblies.

For information about building this example from the command line for [!INCLUDEvbprvb] or [!INCLUDEcsprcs], see Building from the Command Line or Command-line Building With csc.exe. You can also build this example in [!INCLUDEvsprvs] by pasting the code into a new project. Also see How to: Compile and Run a Complete Windows Forms Code Example Using Visual Studio.

See Also

BindingSource Component
How to: Handle Errors and Exceptions that Occur with Databinding
How to: Bind a Windows Forms Control to a Type