Add Tables Columns into DataGrid Control in VB.NET
In this article you will learn how to use Adding Tables and Columns to the Windows Forms DataGrid Control.
Adding Tables and Columns to the Windows Forms DataGrid Control
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
-
-
Select the DataGrid control's TableStyles property in the Properties window, and then click the ellipsis button () next to the property to display the DataGridTableStyle Collection Editor.
-
In the collection editor, click the Add button to insert a table style.
-
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.
-
In the Members box of the collection editor, click the table style.
-
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
-
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 () next to the property to display the DataGridColumnStyle Collection Editor.
-
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.
-
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.
-
In the Members box of the collection editor, click the column style.
-
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.
-
-
Declare a new table style and set its mapping name.
-
Declare a new column style and set its mapping name and other properties.
-
Call the Add method of the GridColumnStylesCollection object to add the column to the table style
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)