Udostępnij za pośrednictwem


How to: Bind a Windows Forms Control to a Type Using the Designer

When you are building controls that interact with data, you sometimes need to bind a control to a type, rather than an object. You typically need to bind a control to a type at design time, when data may not be available, but you still want your data-bound controls to display data from a type's public interface. The following procedures demonstrate how to create a new BindingSource that is bound to a type, and then how to bind one of the type's properties to the Text property of a TextBox.

To bind the BindingSource to a type

  1. Create a Windows Forms project.

    For more information, see How to: Create a New Windows Forms Application Project.

  2. In Design view, drag a BindingSource component onto the form.

  3. In the Properties window, click the arrow for the DataSource property.

  4. In the DataSource UI Type Editor, click Add Project Data Source.

  5. On the Choose a Data Source Type page, select Object and click Next.

  6. Select the type to bind to:

    • If the type you want to bind to is in the current project, or the assembly that contains the type is already added as a reference, expand the nodes to find the type you want, and then select it.

      -or-

    • If the type you want to bind to is in another assembly, not currently in the list of references, click Add Reference, and then click the Projects tab. Select the project that contains the business object you want and click OK. This project will appear in the list of assemblies, so you can expand the nodes to find the type you want, and then select it.

      Note

      If you want to bind to a type in a framework or Microsoft assembly, clear the Hide assemblies that begin with Microsoft or System check box.

  7. Click Next, and then click Finish.

To bind the control to the BindingSource

  1. Add a TextBox to the form.

  2. In the Properties window, expand the (DataBindings) node.

  3. Click the arrow next to the Text property.

  4. In the DataSource UI Type Editor, expand the node for the BindingSource added previously, and select the property of the bound type you want to bind to the Text property of the TextBox.

See Also

Tasks

How to: Bind a Windows Forms Control to a Type

Concepts

Binding Controls to Data in Visual Studio

Other Resources

BindingSource Component