Delen via


How to: Bind the Windows Forms DataGrid Control to a Data Source Using the Designer

Note

The DataGridView control replaces and adds functionality to the DataGrid control; however, the DataGrid control is retained for both backward compatibility and future use, if you choose. For more information, see Differences Between the Windows Forms DataGridView and DataGrid Controls.

The Windows Forms DataGrid control is specifically designed to display information from a data source. You bind the control at design time by setting the DataSource and DataMember properties, or at run time by calling the SetDataBinding method. Although you can display data from a variety of data sources, the most typical sources are datasets and data views.

If the data source is available at design time—for example, if the form contains an instance of a dataset or a data view—you can bind the grid to the data source at design time. You can then preview what the data will look like in the grid.

You can also bind the grid programmatically, at run time. This is useful when you want to set a data source based on information you get at run time. For example, the application might let the user specify the name of a table to view. It is also necessary in situations where the data source does not exist at design time. This includes data sources such as arrays, collections, untyped datasets, and data readers.

The following procedure requires a Windows Application project with a form containing a DataGrid control. For information about setting up such a project, see How to: Create a Windows Application Project and How to: Add Controls to Windows Forms. In Visual Studio 2005, the DataGrid control is not in the Toolbox by default. For information about adding it, see How to: Add Items to the Toolbox. Additionally in Visual Studio 2005, you can use the Data Sources window for design-time data binding. For more information see Displaying Data on Forms in Windows Applications.

Note

The dialog boxes and menu commands you see might differ from those described in Help depending on your active settings or edition. To change your settings, choose Import and Export Settings on the Tools menu. For more information, see Visual Studio Settings.

To data-bind the DataGrid control to a single table in the designer

  1. Set the control's DataSource property to the object containing the data items you want to bind to.

  2. If the data source is a dataset, set the DataMember property to the name of the table to bind to.

  3. If the data source is a dataset or a data view based on a dataset table, add code to the form to fill the dataset.

    The exact code you use depends on where the dataset is getting data. If the dataset is being populated directly from a database, you typically call the Fill method of a data adapter, as in the following code example, which populates a dataset called DsCategories1:

    sqlDataAdapter1.Fill(DsCategories1)
    
    sqlDataAdapter1.Fill(DsCategories1);
    
    sqlDataAdapter1->Fill(dsCategories1);
    
  4. (Optional) Add the appropriate table styles and column styles to the grid.

    If there are no table styles, you will see the table, but with minimal formatting and with all columns visible.

To data-bind the DataGrid control to multiple tables in a dataset in the designer

  1. Set the control's DataSource property to the object containing the data items you want to bind to.

  2. If the dataset contains related tables (that is, if it contains a relation object), set the DataMember property to the name of the parent table.

  3. Write code to fill the dataset.

See Also

Tasks

How to: Add Tables and Columns to the Windows Forms DataGrid Control

Reference

DataGrid Control Overview (Windows Forms)

Other Resources

DataGrid Control (Windows Forms)

Walkthrough: Creating a Distributed Application

Windows Forms Data Binding

Accessing Data (Visual Studio)