Defining Table and Column Styles
Notice that the DataGrid control shows all the columns in the data source and defaults to the column name for the header text. Not too badyou thinkin ASP.NET too this is the default. There should be a Boolean property to disable this feature. And there should be some sort of collection to add your own columns. The key object is a collection with the name you don't expectthe TableStyles
|A DataGrid can display more than one table if the tables are children of the same DataSet and hierarchically related.|
Data displayed in the Windows Forms DataGrid is grouped in tables and columns. A DataGrid can display more than one table if the tables are children of the same DataSet and hierarchically related. Each table can have its own style thereby justifying the TableStyles collection. The collection is created by the DataGrid constructor and is left empty by default. An empty TableStyles collection causes the grid to display as many columns as there are fields in the DataTable. This behavior is nearly identical to the AutoGenerateColumns
property of the ASP.NET DataGrid.
To restrict the number of displayed columns, you add ColumnStyle objects to the TableStyles collection.
Dim gridStyle As DataGridTableStyle
gridStyle = New DataGridTableStyle()
gridStyle.MappingName = "Employees"
gridStyle.BackColor = Color.Beige
is a property declared of type GridTableStylesCollection. It takes items of type DataGridTableStyle. Multiple TableStyle objects are allowedideally one per each table bound to the grid. The mapping between the DataGridTableStyle object and one of the bound tables is set in the MappingName
property. Whenever the specified table is selected for view in the grid, the linked styles are applied.
A TableStyle object features several visual properties including colors, line styles, and preferred width. More important, a TableStyle object contains a collection of ColumnStyle objectsthe GridColumnStyles
You populate the GridColumnStyles collection with objects of type DataGridColumnStyle. This class represents the base abstract class for DataGrid columns. If you want to create a custom type of column, this class is your starting point. In truth, you will probably never use the DataGridColumnStyle class but one of its derived classesDataGridTextBoxColumn
Dim col1 As DataGridTextBoxColumn
col1 = New DataGridTextBoxColumn()
col1.TextBox.Enabled = False
col1.Format = "[#0]"
col1.Alignment = HorizontalAlignment.Right
col1.HeaderText = "ID"
col1.MappingName = "employeeid"
Dim col2 As DataGridTextBoxColumn
col2 = New DataGridTextBoxColumn()
col2.HeaderText = "Last Name"
col2.MappingName = "lastname"
The code snippet above shows you how to create a couple of columns to render the employee ID and the last name. In the former case, the column is right-aligned, read-only, and with a special formatting string. The MappingName
property establishes the link between the column in the grid and a column in the DataTable mapped to the parent table style's object. The property HeaderText
denotes the text that will be displayed as the title of the column.
You cannot set many visual properties on a column object. You can set the width of the column, its mapping, and its alignment, but not font names, font styles, or colors. The MappingName
property must exactly match the name of a table column. If your query returns first and last name as two distinct columns and you want to see them displayed as a single field, try the following: Add an in-memory new column to the table and make it an expression-based column. No further data is ever transmitted from the database to the application and the value for each row is determined by dynamically evaluating the expression. The following code snippet shows you how to concatenate first and last name into a single field called DisplayName.
Dim cols As DataColumnCollection
cols.Add("DisplayName", GetType(String), _
"lastname + ', ' + firstname")
I create and add the new column as soon as the DataTable is generated and before the column configuration takes place.
The DataGridTextBoxColumn supports in-place editing and exposes the TextBox
property in case you want to use the underlying TextBox interface to interact. The content displayed in a column can be formatted to some extent. Use numeric placeholders to identify the cell value and then add formatting information as needed. For example, the expression [#0] indicates the i.th element bracketed by square brackets (see Figure 2
). The property of interest is Format