How to: Display Data by Using DataGridView Controls

Visual Studio 2008

You can display data in your application by using a data-bound DataGridView control. First you add a data source to your application, and then you can drag a DataGridView control from the Data Sources window to your application. To learn how to connect to a database, see How to: Connect to a Database (C#).

You can also display data in individual controls, such as a TextBox control. For more information, see How to: Display Data on a Form.

To display data on a DataGridView control

  1. On the File menu, click New Project.

  2. In the New Project dialog box, click Windows Forms Application, and then click OK.

    A new Windows Forms project opens.

  3. Add a connection to the sample database named Northwind.sdf as described in How to: Connect to a Database (C#).

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

    The Data Sources window opens.

  5. In the Data Sources window, expand NorthwindDataSet, and then click the Customers table.

  6. Click the drop-down arrow next to the Customers table and then click DataGridView.

  7. Drag the Customers table from the Data Sources window to form1.

    A DataGridView control and a tool strip (BindingNavigator) for navigating records appear on the form. You will also see a DataSet, TableAdapter, BindingSource, and BindingNavigator in the component tray.

  8. Click the DataGridView control and change its Dock property to Fill by clicking the arrow to the right of the Dock property and clicking the center square.

  9. Press F5 to run the code.

  10. Resize the form and scroll through the data in the DataGridView control.

Community Additions