Files
docs-desktop/dotnet-desktop-guide/framework/winforms/controls/how-to-handle-errors-and-exceptions-that-occur-with-databinding.md
T
Andy De George c0ba284473 Initial winforms content migrated (#18)
* Merge winforms framework content to working branch (#14)

* Breadcrumb / TOC / Move net5 to net folder (#15)

* change path from net5 to net

* Mess with bread/toc

* Mess with bread/toc

* Mess with bread/toc

* Mess with bread/toc

* Mess with bread/toc

* Mess with bread/toc

* Mess with bread/toc

* Mess with bread/toc

* Mess with bread/toc

* Mess with bread/toc

* Mess with bread/toc

* Mess with bread/toc

* Mess with bread/toc

* Mess with bread/toc

* Add .net 5 winforms placeholder article (#16)

* added some metadata and adjusted net5 placeholder

* corrections

* corrections

* corrections

* Test1

* Swapping landing page vs concept

* fix links

* Fix links

* Fix desc
2020-09-01 16:26:21 -07:00

2.8 KiB

title, ms.date, dev_langs, helpviewer_keywords, ms.assetid
title ms.date dev_langs helpviewer_keywords ms.assetid
How to: Handle Errors and Exceptions that Occur with Databinding 03/30/2017
csharp
vb
cpp
error handling [Windows Forms], examples
data binding [Windows Forms], examples
examples [Windows Forms], error handling
error handling [Windows Forms], data binding
data binding [Windows Forms], error handling
BindingSource component [Windows Forms], handling errors and exceptions
eddc5bad-9513-47df-ab28-f02d8dff7892

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.

See also