Export (0) Print
Expand All

Adding Tables and Columns to the Windows Forms DataGrid Control

Visual Studio .NET 2003

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 DataGridTableStyle object's MappingName property. By default, a table style with no 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 object, which is accessed through the GridColumnStyles property of each DataGridTableStyle object.

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 Binding the Windows Forms DataGrid Control to a Data Source.
  2. Select the DataGrid control's TableStyles property in the Properties window, and then click the ellipsis button (Aa984314.vbellipsesbutton(en-us,VS.71).gif) next to the property to display the DataGridTableStyle Collection Editor.
  3. In the collection editor, click the Add button to insert a table style.
  4. Click OK to close the collection editor, then re-open 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 wish 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 (Aa984314.vbellipsesbutton(en-us,VS.71).gif) next to the property to display the DataGridColumnStyle Collection Editor.
  2. In the collection editor, click the Add button to insert a column style or click the down arrow next to the Add button to specify a column type. The drop-down box will allow you to select either the DataGridTextBoxColumn or DataGridBoolColumn type.
  3. Click OK to close the DataGridColumnStyle Collection Editor, then re-open 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 wish to display.

To add a table and column to a DataGrid programmatically

Caution   When programmatically specifying column styles, always create DataGridColumnStyle objects and add them to the GridColumnStylesCollection object before adding DataGridTableStyle objects to the GridTableStylesCollection object. When you add an empty DataGridTableStyle object to the collection, DataGridColumnStyle objects are automatically generated for you. Consequently, an exception will be thrown if you try to add new DataGridColumnStyle objects with duplicate MappingName values to the GridColumnStylesCollection object.
  1. In order to display data in the table, you must first bind the DataGrid control to a dataset. For more information, see Binding the Windows Forms DataGrid Control to a Data Source.
  2. Declare a new table style and set its mapping name.
    'Visual Basic
    Dim ts1 As New DataGridTableStyle()
    ts1.MappingName = "Customers"
    
    // C#
    DataGridTableStyle ts1 = new DataGridTableStyle();
    ts1.MappingName = "Customers";
    
    // C++
    DataGridTableStyle* ts1 = new DataGridTableStyle();
    ts1->MappingName = S"Customers";
    
  3. Declare a new column style and set its mapping name and other properties.
    'Visual Basic
    Dim myDataCol As New DataGridBoolColumn()
    myDataCol.HeaderText = "My New Column"
    myDataCol.MappingName = "Current"
    
    // C#
    DataGridBoolColumn myDataCol = new DataGridBoolColumn();
    myDataCol.HeaderText = "My New Column";
    myDataCol.MappingName = "Current";
    
    // C++
    DataGridBoolColumn* myDataCol = new DataGridBoolColumn();
    myDataCol->HeaderText = S"My New Column";
    myDataCol->MappingName = S"Current";
    
  4. Call the Add method of the GridColumnStylesCollection object to add the column to the table style
    'Visual Basic
    ts1.GridColumnStyles.Add(myDataCol)
    
    // C#
    ts1.GridColumnStyles.Add(myDataCol);
    
    // C++
    ts1->GridColumnStyles->Add(myDataCol);
    
  5. Call the Add method of the GridTableStylesCollection object to add the table style to the data grid.
    'Visual Basic
    DataGrid1.TableStyles.Add(ts1)
    
    // C#
    dataGrid1.TableStyles.Add(ts1);
    
    // C++
    dataGrid1->TableStyles->Add(ts1);
    

See Also

DataGrid Control (Windows Forms) | Deleting Columns in the Windows Forms DataGrid Control

Show:
© 2014 Microsoft