title | description | ms.date | dev_langs | helpviewer_keywords | ms.assetid | ||||||
---|---|---|---|---|---|---|---|---|---|---|---|
Add Tables and Columns to DataGrid Control |
Learn how to add tables and columns to a DataGrid control. You can restrict which columns from the table appear. |
03/30/2017 |
|
|
2fe661b9-aa06-49b9-a314-a0d3cbfdcb4d |
Note
The xref:System.Windows.Forms.DataGridView control replaces and adds functionality to the xref:System.Windows.Forms.DataGrid control; however, the xref:System.Windows.Forms.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 xref:System.Windows.Forms.DataGrid control in tables and columns by creating DataGridTableStyle objects and adding them to the GridTableStylesCollection object, which is accessed through the xref:System.Windows.Forms.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.
-
In order to display data in the table, you must first bind the xref:System.Windows.Forms.DataGrid control to a dataset. For more information, see How to: Bind the Windows Forms DataGrid Control to a Data Source.
[!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.
-
Declare a new table style and set its mapping name.
Dim ts1 As New DataGridTableStyle() ts1.MappingName = "Customers"
DataGridTableStyle ts1 = new DataGridTableStyle(); ts1.MappingName = "Customers";
DataGridTableStyle* ts1 = new DataGridTableStyle(); ts1->MappingName = S"Customers";
-
Declare a new column style and set its mapping name and other properties.
Dim myDataCol As New DataGridBoolColumn() myDataCol.HeaderText = "My New Column" myDataCol.MappingName = "Current"
DataGridBoolColumn myDataCol = new DataGridBoolColumn(); myDataCol.HeaderText = "My New Column"; myDataCol.MappingName = "Current";
DataGridBoolColumn^ myDataCol = gcnew DataGridBoolColumn(); myDataCol->HeaderText = "My New Column"; myDataCol->MappingName = "Current";
-
Call the Add method of the GridColumnStylesCollection object to add the column to the table style
ts1.GridColumnStyles.Add(myDataCol)
ts1.GridColumnStyles.Add(myDataCol);
ts1->GridColumnStyles->Add(myDataCol);
-
Call the Add method of the GridTableStylesCollection object to add the table style to the data grid.
DataGrid1.TableStyles.Add(ts1)
dataGrid1.TableStyles.Add(ts1);
dataGrid1->TableStyles->Add(ts1);