Data Controls: Displaying Data in DataGridView Controls

If you want to give users a way to view data in a grid format, the DataGridView control is the best choice. This is especially true if there is a lot of data in the table, because you can set the height and width of the control on the form, yet still enable users to see all the data.

Try It!

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 Getting the Information You Need: Connecting to an Existing Database.

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

  5. The Data Sources window opens.

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

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

  8. Drag the Customers table from the Data Sources window to Form1.

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

  9. Select the DataGridView control and change its Dock property to Fill by clicking the arrow to the right of the Dock property and then clicking the center square.

  10. Press F5 to run the code.

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

Next Steps

In this lesson, you learned how to display data from a database in a Windows Form by using a DataGridView control. In the next lesson, you will learn how to use components in your application.

Next Lesson: Invisible Controls: Using Components.

See Also

Tasks

Creating Your First Database

Getting the Information You Need: Connecting to an Existing Database

Other Resources

Creating the Visual Look of Your Program: Introduction to Windows Forms

Managing Your Records: Using Data in Your Program