Udostępnij za pośrednictwem


How to: Add Tables and Columns to the Windows Forms DataGrid Control 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.

You can display data in the Windows Forms DataGrid control in tables and columns by creating DataGridTableStyle objects and adding them to the GridTableStylesCollection object, which is accessed through the DataGrid control's TableStyles property. Each table style displays the contents of whatever data table is specified in the MappingName property of the DataGridTableStyle. By default, a table style without column styles specified will display all the columns within that data table. You can restrict which columns from the table appear by adding DataGridColumnStyle objects to the GridColumnStylesCollection, which is accessed through the GridColumnStyles property of each DataGridTableStyle.

The following procedures require a Windows Application project with a form that contains a DataGrid control. For information about how to set up such a project, see How to: Create a Windows Application Project and How to: Add Controls to Windows Forms. By default in Visual Studio 2005, the DataGrid control is not in the Toolbox. For information about adding it, see How to: Add Items to the Toolbox.

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 add a table to the DataGrid control in the designer

  1. In order to display data in the table, you must first bind the DataGrid control to a dataset. For more information, see How to: Bind the Windows Forms DataGrid Control to a Data Source Using the Designer.

  2. Select the DataGrid control's TableStyles property in the Properties window, and then click the ellipsis button (VisualStudioEllipsesButton screenshot) next to the property to display the DataGridTableStyle Collection Editor.

  3. In the collection editor, click Add to insert a table style.

  4. Click OK to close the collection editor, and then reopen it by clicking the ellipsis button next to the TableStyles property.

    When you reopen the collection editor, any data tables bound to the control will appear in the drop-down list for the MappingName property of the table style.

  5. In the Members box of the collection editor, click the table style.

  6. In the Properties box of the collection editor, select the MappingName value for the table you want to display.

To add a column to the DataGrid control in the designer

  1. In the Members box of the DataGridTableStyle Collection Editor, select the appropriate table style. In the Properties box of the collection editor, select the GridColumnStyles collection, and then click the ellipsis button (VisualStudioEllipsesButton screenshot) next to the property to display the DataGridColumnStyle Collection Editor.

  2. In the collection editor, click Add to insert a column style or click the down arrow next to Add to specify a column type.

    In the drop-down box, you can select either the DataGridTextBoxColumn or DataGridBoolColumn type.

  3. Click OK to close the DataGridColumnStyle Collection Editor, and then reopen it by clicking the ellipsis button next to the GridColumnStyles property.

    When you reopen the collection editor, any data columns in the bound data table will appear in the drop-down list for the MappingName property of the column style.

  4. In the Members box of the collection editor, click the column style.

  5. In the Properties box of the collection editor, select the MappingName value for the column you want to display.

See Also

Tasks

How to: Delete or Hide Columns in the Windows Forms DataGrid Control

Other Resources

DataGrid Control (Windows Forms)