Freigeben über


Walkthrough: Creating a Windows Forms User Control that Supports Complex Data Binding

When displaying data on forms in Windows applications, you can choose existing controls from the Toolbox, or you can author custom controls if your application requires functionality that is not available in the standard controls. This walkthrough shows how to create a control that implements the ComplexBindingPropertiesAttribute. Controls that implement the ComplexBindingPropertiesAttribute contain a DataSource and DataMember property that can be bound to data. Such controls are similar to a DataGridView or ListBox.

For more information on control authoring, see Developing Windows Forms Controls at Design Time.

When authoring controls for use in databinding scenarios you need to implement one of the following data-binding attributes:

Databinding Attribute Usage

Implement the DefaultBindingPropertyAttribute on simple controls, like a TextBox, that display a single column (or property) of data. For more information, see Walkthrough: Creating a Windows Forms User Control that Supports Simple Data Binding.

Implement the ComplexBindingPropertiesAttribute on controls, like a DataGridView, that display lists (or tables) of data. (This process is described in this walkthrough page.)

Implement the LookupBindingPropertiesAttribute on controls, like a ComboBox, that display lists (or tables) of data but also need to present a single column or property. For more information, see Walkthrough: Creating a Windows Forms User Control that Supports Lookup Databinding.

This walkthrough creates a complex control that displays rows of data from a table. This example uses the Customers table from the Northwind sample database. The complex user control will display the customers table in a DataGridView in the custom control.

During this walkthrough, you will learn how to:

  • Create a new Windows Application.

  • Add a new User Control to your project.

  • Visually design the user control.

  • Implement the ComplexBindingProperty attribute.

  • Create a dataset with the Data Source Configuration Wizard.

  • Set the Customers table in the Data Sources Window to use the new complex control.

  • Add the new control by dragging it from the Data Sources Window onto Form1.

Prerequisites

In order to complete this walkthrough, you will need:

Creating a Windows Application

The first step is to create a Windows Application.

To create the new Windows project

  1. In Visual Studio, from the File menu, create a new Project.

  2. Name the project ComplexControlWalkthrough.

  3. Select Windows Application and click OK. For more information, see Developing Client Applications with the .NET Framework.

    The ComplexControlWalkthrough project is created and added to Solution Explorer.

Adding a User Control to the Project

Because this walkthrough creates a complex data-bindable control from a User Control, you must add a User Control item to the project.

To add a user control to the project

  1. From the Project menu, choose Add User Control.

  2. Type ComplexDataGridView in the Name area, and then click Add.

    The ComplexDataGridView control is added to Solution Explorer and opens in the designer.

Designing the ComplexDataGridView Control

This step adds a DataGridView to the user control.

To design the ComplexDataGridView control

  • Drag a DataGridView from the Toolbox onto the user control's design surface.

Adding the Required Data-binding Attribute

For complex controls that support data binding, you can implement the ComplexBindingPropertiesAttribute.

To implement the ComplexBindingProperties attribute

  1. Switch the ComplexDataGridView control to code view. (On the View menu, select Code.)

  2. Replace the code in the ComplexDataGridView with the following:

    <System.ComponentModel.ComplexBindingProperties("DataSource", "DataMember")>
    Public Class ComplexDataGridView
    
        Public Property DataSource() As Object 
            Get 
                Return DataGridView1.DataSource
            End Get 
            Set(ByVal value As Object)
                DataGridView1.DataSource = value
            End Set 
        End Property 
    
        Public Property DataMember() As String 
            Get 
                Return DataGridView1.DataMember
            End Get 
            Set(ByVal value As String)
                DataGridView1.DataMember = value
            End Set 
        End Property 
    End Class
    
    using System.Windows.Forms;
    
    namespace CS
    {
        [System.ComponentModel.ComplexBindingProperties("DataSource", "DataMember")]
        public partial class ComplexDataGridView : UserControl
        {
            public object DataSource
            {
                get{ return dataGridView1.DataSource; }
                set{ dataGridView1.DataSource = value; }
            }
    
            public string DataMember
            {
                get{ return dataGridView1.DataMember; }
                set{ dataGridView1.DataMember = value; }
            }
    
            public ComplexDataGridView()
            {
                InitializeComponent();
            }
        }
    }
    
  3. From the Build menu, choose Build Solution.

Creating a Data Source from Your Database

This step uses the Data Source Configuration Wizard to create a data source based on the Customers table in the Northwind sample database. You must have access to the Northwind sample database to create the connection. For information on setting up the Northwind sample database, see How to: Install Sample Databases.

To create the Data Source

  1. On the Data menu, click Show Data Sources.

  2. In the Data Sources window, select Add New Data Source to start the Data Source Configuration Wizard.

  3. Select Database on the Choose a Data Source Type page, and then click Next.

  4. On the Choose your Data Connection page do one of the following:

    • If a data connection to the Northwind sample database is available in the drop-down list, select it.

      -or-

    • Select New Connection to launch the Add/Modify Connection dialog box.

  5. If your database requires a password, select the option to include sensitive data, and then click Next.

  6. Click Next on the Save connection string to the Application Configuration file page.

  7. Expand the Tables node on the Choose your Database Objects page.

  8. Select the Customers table, and then click Finish.

    The NorthwindDataSet is added to your project and the Customers table appears in the Data Sources window.

Setting the Customers Table To Use the ComplexDataGridView Control

Within the Data Sources window you can set the control to be created prior to dragging items onto your form.

To set the Customers table to bind to the ComplexDataGridView control

  1. Open Form1 in the designer.

  2. Expand the Customers node in the Data Sources window.

  3. Click the drop-down arrow on the Customers node and choose Customize.

  4. Select the ComplexDataGridView from the list of Associated Controls in the Data UI Customization Options dialog box.

  5. Click the drop-down arrow on the Customers table and choose ComplexDataGridView from the control list.

Adding Controls to the Form

You can create the data-bound controls by dragging items from the Data Sources window onto your form.

To create data-bound controls on the form

  • Drag the main Customers node from the Data Sources window onto the form and verify that the ComplexDataGridView control is used to display the table's data.

Running the Application

To run the application

  • Press F5 to run the application.

Next Steps

Depending on your application requirements, there are several steps you may want to perform after creating a control that supports databinding. Some typical next steps include:

See Also

Tasks

How to: Set the Control to be Created when Dragging from the Data Sources Window

Concepts

Binding Windows Forms Controls to Data in Visual Studio

Preparing Your Application to Receive Data

Fetching Data into Your Application

Binding Controls to Data in Visual Studio

Editing Data in Your Application

Validating Data

Saving Data

Other Resources

Windows Forms Controls

Connecting to Data in Visual Studio