Permalink
Fetching contributors…
Cannot retrieve contributors at this time
50 lines (42 sloc) 3.33 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 a Windows Forms Control to a Factory Object
03/30/2017
.net-framework
dotnet-winforms
article
jsharp
controls [Windows Forms], binding
factory objects, binding to
BindingSource component [Windows Forms], binding to a factory object
BindingSource component [Windows Forms], examples
7d59af89-ff82-41d8-a48a-f1fbae788b0d
12
dotnet-bot
dotnetcontent
wpickett

How to: Bind a Windows Forms Control to a Factory Object

When you are building controls that interact with data, you will sometimes find it necessary to bind a control to an object or method that generates other objects. Such an object or method is called a factory. Your data source might be, for example, the return value from a method call, instead of an object in memory or a type. You can bind a control to this kind of data source as long as the source returns a collection.

You can easily bind a control to a factory object by using the xref:System.Windows.Forms.BindingSource control.

Example

The following example demonstrates how to bind a xref:System.Windows.Forms.DataGridView control to a factory method by using a xref:System.Windows.Forms.BindingSource control. The factory method is named GetOrdersByCustomerId, and it returns all the orders for a given customer in the Northwind database.

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

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

xref:System.Windows.Forms.BindingNavigator
xref:System.Windows.Forms.DataGridView
xref:System.Windows.Forms.BindingSource
BindingSource Component
How to: Bind a Windows Forms Control to a Type