| 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 Type |
03/30/2017 |
.net-framework |
|
article |
|
|
94faeebb-d2bc-45d6-86d7-96a42661b43d |
19 |
dotnet-bot |
dotnetcontent |
wpickett |
How to: Bind a Windows Forms Control to a Type
When you are building controls that interact with data, you will sometimes find it necessary to bind a control to a type, rather than an object. This situation arises especially at design time, when data may not be available, but your data-bound controls still need to display information from a type's public interface. For example, you may bind a xref:System.Windows.Forms.DataGridView control to an object exposed by a Web service and want the xref:System.Windows.Forms.DataGridView control to label its columns at design time with the member names of a custom type.
You can easily bind a control to a type with the xref:System.Windows.Forms.BindingSource component.
Example
The following code example demonstrates how to bind a xref:System.Windows.Forms.DataGridView control to a custom type by using a xref:System.Windows.Forms.BindingSource component. When you run the example, you'll notice the xref:System.Windows.Forms.DataGridView has labeled columns that reflect the properties of a Customer object, before the control is populated with data. The example has an Add Customer button to add data to the xref:System.Windows.Forms.DataGridView control. When you click the button, a new Customer object is added to the xref:System.Windows.Forms.BindingSource. In a real-world scenario, the data might be obtained by a call to a Web service or other data source.
[!code-csharpSystem.Windows.Forms.DataConnector.BindingToType#1] [!code-vbSystem.Windows.Forms.DataConnector.BindingToType#1]
Compiling the Code
This example requires:
- References to the System 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