How to: Bind Windows Forms Controls to Data
Bind data to Windows Forms controls by dragging objects from the Data Sources Window. Before you drag items from the Data Sources window, you can set the control type of the table to Details for individual controls, or DataGridView for a DataGridView. For more information, see How to: Set the Control to be Created when Dragging from the Data Sources Window.
If the controls needed by your application are not available from within the Data Sources window, you can add controls. For more information, see How to: Add Custom Controls to the Data Sources Window.
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.
Displaying an Entire Table of Data in Individual Controls
You can display an entire table of data in individual controls by dragging the table (or a node representing a collection if you are using an object data source) from the Data Sources window onto a form in a Windows application.
To display an entire table of data
Open the Data Sources window. For more information, see How to: Open the Data Sources Window.
Note
If the Data Sources window is empty, add a data source to it. For more information, see Data Sources Overview.
Open the form in the Windows Forms Designer.
Select a table in the Data Sources window, click the drop-down arrow, and then select Details.
Drag the table from the Data Sources window onto a form.
An individual data-bound control for each column or property is created on the form, accompanied by an appropriately titled Label control.
Displaying Selected Columns of Data in Individual Controls
Display individual columns of data in individual controls by dragging the individual columns (or properties if you are using an object data source) from the Data Sources window onto a form in a Windows application.
To display select columns of data
Open the Data Sources window. For more information, see How to: Open the Data Sources Window.
Note
If the Data Sources window is empty, add a data source to it. For more information, see Data Sources Overview.
Expand the table to display the individual columns.
Tip
To set the control that is created for each column, select the column in the Data Sources window, click the drop-down arrow, and select a control from the list of available controls. For more information, see How to: Set the Control to be Created when Dragging from the Data Sources Window.
Open the form in the Windows Forms Designer.
Drag the desired columns from the Data Sources window onto a form.
For each column or property you drag, an individual data-bound control is created on the form, accompanied by an appropriately titled label control.
You can also drag items from the Data Sources window onto existing controls (controls already on a form) to bind the control to data. A control that is already bound to data has its data bindings reset to the item most recently dragged onto it.
Note
To be valid drop targets, controls must be capable of displaying the underlying data type of the item dragged onto it from the Data Sources window. For example, it is not valid to drag an item that has a data type of DateTime onto a CheckBox, because the CheckBox is not capable of displaying a date.
To bind an existing control to data
Open the Data Sources window. For more information, see How to: Open the Data Sources Window.
Open the form in the Windows Forms Designer.
Expand a table or object in the Data Sources window to display its individual columns or properties.
Drag the desired item from the Data Sources window onto an existing control.
The control is now bound to that selected item.
Displaying Data in a DataGridView Control
To display data in a new Windows Forms DataGridView control
Open the Data Sources window. For more information, see How to: Open the Data Sources Window.
Note
If the Data Sources window is empty, then add a data source to it. For more information, see Data Sources Overview.
Open the form in the Windows Forms Designer.
Select a table in the Data Sources window, click the drop-down arrow, and then select DataGridView.
Drag the table from the Data Sources window to a form.
A DataGridView control and a tool strip (BindingNavigator) for navigating records appear on the form. A DataSet, TableAdapter, BindingSource, and BindingNavigator appear in the component tray.
To display data in an existing Windows Forms DataGridView control
Open the Data Sources window. For more information, see How to: Open the Data Sources Window.
Note
If the Data Sources window is empty, then add a data source to it. For more information, see Data Sources Overview.
Open your form in the Windows Forms Designer.
Select a table in the Data Sources window, click the drop-down arrow, and then select DataGridView.
Drag the table from the Data Sources window onto a DataGridView on the form.
The DataGridView control is now bound to the table that you have dragged onto it. A DataSet, TableAdapter, and BindingSource appear in the component tray.
See Also
Tasks
Walkthrough: Displaying Data on a Windows Form
Reference
BindingSource Component Overview
BindingNavigator Control Overview (Windows Forms)
Concepts
Preparing Your Application to Receive Data
Fetching Data into Your Application
Binding Controls to Data in Visual Studio
Editing Data in Your Application