| 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: Handle Errors and Exceptions that Occur with Databinding |
03/30/2017 |
.net-framework |
|
article |
|
|
eddc5bad-9513-47df-ab28-f02d8dff7892 |
15 |
dotnet-bot |
dotnetcontent |
wpickett |
How to: Handle Errors and Exceptions that Occur with Databinding
Oftentimes exceptions and errors occur on the underlying business objects when you bind them to controls. You can intercept these errors and exceptions and then either recover or pass the error information to the user by handling the xref:System.Windows.Forms.Binding.BindingComplete event for a particular xref:System.Windows.Forms.Binding, xref:System.Windows.Forms.BindingSource, or xref:System.Windows.Forms.CurrencyManager component.
Example
This code example demonstrates how to handle errors and exceptions that occur during a data-binding operation. It demonstrates how to intercept errors by handling the xref:System.Windows.Forms.Binding.BindingComplete?displayProperty=nameWithType event of the xref:System.Windows.Forms.Binding objects. In order to intercept errors and exceptions by handling this event, you must enable formatting for the binding. You can enable formatting when the binding is constructed or added to the binding collection, or by setting the xref:System.Windows.Forms.Binding.FormattingEnabled%2A property to true.
[!code-cppSystem.Windows.Forms.DataConnectorBindingComplete#3] [!code-csharpSystem.Windows.Forms.DataConnectorBindingComplete#3] [!code-vbSystem.Windows.Forms.DataConnectorBindingComplete#3]
When the code is running and an empty string is entered for the part name or a value less than 100 is entered for the part number, a message box appears. This is a result of handling the xref:System.Windows.Forms.Binding.BindingComplete?displayProperty=nameWithType event for these textbox bindings.
Compiling the Code
This example requires:
- References to the System, 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
xref:System.Windows.Forms.Binding.BindingComplete?displayProperty=nameWithType
xref:System.Windows.Forms.BindingSource.BindingComplete?displayProperty=nameWithType
BindingSource Component