Files
docs-desktop/dotnet-desktop-guide/framework/winforms/controls/how-to-customize-item-addition-with-the-windows-forms-bindingsource.md
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.9 KiB

title, ms.date, dev_langs, helpviewer_keywords, ms.assetid
title ms.date dev_langs helpviewer_keywords ms.assetid
Customize Item Addition with BindingSource component 03/30/2017
csharp
vb
cpp
controls [Windows Forms], creating new items
BindingSource component [Windows Forms], customizing item addition with
examples [Windows Forms], BindingSource component
BindingSource component [Windows Forms], examples
1aae11fc-6fb2-4cb9-b3d0-e0638fe77ef0

How to: Customize Item Addition with the Windows Forms BindingSource

When you use a xref:System.Windows.Forms.BindingSource component to bind a Windows Forms control to a data source, you may find it necessary to customize the creation of new items. The xref:System.Windows.Forms.BindingSource component makes this straightforward by providing the xref:System.Windows.Forms.BindingSource.AddingNew event, which is typically raised when the bound control needs to create a new item. Your event handler can provide whatever custom behavior is required (for example, calling a method on a Web service or getting a new object from a class factory).

Note

When an item is added by handling the xref:System.Windows.Forms.BindingSource.AddingNew event, the addition cannot be canceled.

Example

The following example demonstrates how to bind a xref:System.Windows.Forms.DataGridView control to a class factory by using a xref:System.Windows.Forms.BindingSource component. When the user clicks the xref:System.Windows.Forms.DataGridView control's new row, the xref:System.Windows.Forms.BindingSource.AddingNew event is raised. The event handler creates a new DemoCustomer object, which is assigned to the xref:System.ComponentModel.AddingNewEventArgs.NewObject%2A?displayProperty=nameWithType property. This causes the new DemoCustomer object to be added to the xref:System.Windows.Forms.BindingSource component's list and to be displayed in the new row of the xref:System.Windows.Forms.DataGridView control.

[!code-cppSystem.Windows.Forms.DataConnector.AddingNew#1] [!code-csharpSystem.Windows.Forms.DataConnector.AddingNew#1] [!code-vbSystem.Windows.Forms.DataConnector.AddingNew#1]

Compiling the Code

This example requires:

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

See also