Szerkesztés

Megosztás a következőn keresztül:


Bind controls to data in .NET Framework applications using Visual Studio

Note

Datasets and related classes are legacy .NET Framework technologies from the early 2000s that enable applications to work with data in memory while the applications are disconnected from the database. The technologies are especially useful for applications that enable users to modify data and persist the changes back to the database. Although datasets have proven to be a very successful technology, we recommend that new .NET applications use Entity Framework Core. Entity Framework provides a more natural way to work with tabular data as object models, and it has a simpler programming interface.

You can display data to users of your application by binding data to controls. You can create these data-bound controls by dragging items from the Data Sources window onto a design surface or controls on a surface in Visual Studio.

This topic describes the data sources you can use to create data-bound controls. It also describes some of the general tasks involved in data binding. For more specific details about how to create data-bound controls, see Bind Windows Forms controls to data in Visual Studio and Bind WPF controls to data in Visual Studio.

Data sources

In the context of data binding, a data source represents the data in memory that can be bound to your user interface. In practical terms, a data source can be an Entity Framework class, a dataset, a service endpoint that is encapsulated in a .NET proxy object, a LINQ to SQL class, or any .NET object or collection. Some data sources enable you to create data-bound controls by dragging items from the Data Sources window, while other data sources do not. The following table shows which data sources are supported.

Data source Drag-and-drop support in the Windows Forms Designer Drag-and-drop support in the WPF Designer Drag-and-drop support in the Silverlight Designer
Dataset Yes Yes No
Entity Data Model Yes1 Yes Yes
LINQ to SQL classes No2 No2 No2
Services (including WCF Data Services, WCF services, and web services) Yes Yes Yes
Object Yes Yes Yes
SharePoint Yes Yes Yes
  1. Generate the model using the Entity Data Model wizard, then drag those objects to the designer.

  2. LINQ to SQL classes do not appear in the Data Sources window. However, you can add a new object data source that is based on LINQ to SQL classes, and then drag those objects to the designer to create data-bound controls. For more information, see Walkthrough: Creating LINQ to SQL Classes (O-R Designer).

Data Sources window

Data sources are available to your project as items in the Data Sources window. This window is visible when a form design surface is the active window in your project, or you can open it (when a project is open) by choosing View > Other Windows > Data Sources. You can drag items from this window to create controls that are bound to the underlying data, and you can also configure the data sources by right-clicking.

Data Sources window

For each data type that appears in the Data Sources window, a default control is created when you drag the item to the designer. Before you drag an item from the Data Sources window, you can change the control that is created. For more information, see Set the control to be created when dragging from the Data Sources window.

Tasks involved in binding controls to data

The following table lists some of the most common tasks you perform to bind controls to data.

Task More information
Open the Data Sources window. Open a design surface in the editor and choose View > Data Sources.
Add a data source to your project. Add new data sources
Set the control that is created when you drag an item from the Data Sources window to the designer. Set the control to be created when dragging from the Data Sources window
Modify the list of controls that are associated with items in the Data Sources window. Add custom controls to the Data Sources window
Create data-bound controls. Bind Windows Forms controls to data in Visual Studio

Bind WPF controls to data in Visual Studio
Bind to an object or collection. Bind objects in Visual Studio
Filter data that appears in the UI. Filter and sort data in a Windows Forms application
Customize captions for controls. Customize how Visual Studio creates captions for data-bound controls