Поделиться через


How to: Connect to Data in Objects

You can connect your application to data in objects by running the Data Source Configuration Wizard and selecting Object as the data source type.

After completing the wizard, a data source based on the selected object is added to your project and is immediately available in the Data Sources Window. You can drag items from the Data Sources window onto a design surface in your project to create controls that are data-bound to the selected properties of your object. For more information, see Binding Controls to Data in Visual Studio.

For more information about using objects as data sources, see Data Sources Overview.

Note

Your computer might show different names or locations for some of the Visual Studio user interface elements in the following instructions. The Visual Studio edition that you have and the settings that you use determine these elements. For more information, see Visual Studio Settings.

Creating an Object Data Source

To create a new data source from an object

  1. On the Data menu, click Add New Data Source.

  2. Select Object on the Choose a Data Source Type page.

  3. On the Select the Data Objects page, expand the nodes in the tree view to locate the objects that you want to bind to. The tree view contains nodes for your project and for assemblies and other projects referenced by your project.

    If you want to bind to an object in an assembly or project that does not appear in the tree view, click Add Reference and use the Add Reference Dialog Box to add a reference to the assembly or project. After you add the reference, the assembly or project is added to the tree view.

    Note

    You may need to build the project that contains your objects before the objects appear in the tree view.

  4. In the tree view, select the check box for the objects you want to bind to.

  5. Click Finish.

    The data source is added to the Data Sources window.

Creating Controls That Are Bound to Your Object

To add functionality to your application

  1. Choose Show Data Sources on the Data menu to display the Data Sources window.

  2. Select an item in the Data Sources window and drag it onto a design surface to create a control that is bound to the properties in your object. Repeat this step as necessary for other items as needed.

    For more information, see Binding Controls to Data in Visual Studio.

    Note

    To support drag-and-drop data binding, objects that implement the ITypedList or IListSource interface must have a default constructor (that is, a parameterless constructor). Otherwise, Visual Studio cannot instantiate the data source object, and it will display an error when you drag the item to the design surface.

See Also

Tasks

Walkthrough: Connecting to Data in Objects (Windows Forms)

Concepts

Binding Controls to Data in Visual Studio

Other Resources

Overview of Data Applications in Visual Studio

Connecting to Data in Visual Studio

Tools for Working with Data Sources in Visual Studio