Login | Register   
RSS Feed
Download our iPhone app
Browse DevX
Sign up for e-mail newsletters from DevX


Customize the Windows Forms DataGrid Control : Page 2

The System.Windows.Forms.DataGrid control is an Excel-like component and designed to display data contained in .NET collections and list objects, including ADO.NET DataTable and DataView objects and arrays. The control provides scrolling capabilities, both vertically and horizontally, column resize, in-place editing, sorting, and even navigation, should your data source contain hierarchical information.

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 bad—you think—in 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 expect—the TableStyles collection.

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 : grid.TableStyles.Add(gridStyle)

TableStyles is a property declared of type GridTableStylesCollection. It takes items of type DataGridTableStyle. Multiple TableStyle objects are allowed—ideally 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 objects—the GridColumnStyles property.

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 classes—DataGridTextBoxColumn and DataGridBoolColumn.

Dim col1 As DataGridTextBoxColumn col1 = New DataGridTextBoxColumn() gridStyle.GridColumnStyles.Add(col1) col1.TextBox.Enabled = False col1.Format = "[#0]" col1.Alignment = HorizontalAlignment.Right col1.HeaderText = "ID" col1.MappingName = "employeeid" Dim col2 As DataGridTextBoxColumn col2 = New DataGridTextBoxColumn() gridStyle.GridColumnStyles.Add(col2) 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.

Comment and Contribute






(Maximum characters: 1200). You have 1200 characters left.



Thanks for your registration, follow us on our social networks to keep up-to-date