Creating Custom Columns in a Windows Forms DataGrid


You need to create a DataGrid having columns with custom formatting and attributes.


Use the DataGridTableStyle class.

The sample code contains two event handlers:


Sets up the sample by creating a DataAdapter and using it to fill a DataTable with data from the ProductID, ProductName, and Discontinued columns of the Products table in the Northwind database. The DataGridTableStyle class is used to define a data grid containing three custom columnstwo text boxes and one check boxcorresponding to the ProductID, ProductName, and Discontinued columns. Finally, the default view of the Products DataTable is bound to the data grid on the form.

Update Button.Click

Uses the DataAdapter created in the Form.Load event handler to update changes made to the Products DataTable back to the database.

The C# code is shown in Example 7-18.

Example 7-18. File: CustomColumnsInDataGridForm.cs

// Namespaces, variables, and constants
using System;
using System.Configuration;
using System.Windows.Forms;
using System.Data;
using System.Data.SqlClient;

private SqlDataAdapter da;
private DataTable dt;

// . . . 

private void CustomColumnsInDataGridForm_Load(object sender,
 System.EventArgs e)
 // Create the DataAdapter.
 String selectCommand = "SELECT ProductID, ProductName, Discontinued " +
 "FROM Products";
 da = new SqlDataAdapter(selectCommand,
 // Use CommandBuilder to handle updates back to the data source.
 SqlCommandBuilder cb = new SqlCommandBuilder(da);
 // Retrieve the table schema.
 dt = new DataTable("Products");
 da.FillSchema(dt, SchemaType.Source);
 // Default the check box column to false.
 dt.Columns["Discontinued"].DefaultValue = false;
 // Fill the table.

 // Define the table for the grid.
 DataGridTableStyle ts = new DataGridTableStyle( );
 ts.MappingName = "Products";

 // Define and add the columns to the grid.
 DataGridTextBoxColumn productIDCol = new DataGridTextBoxColumn( );
 productIDCol.MappingName = "ProductID";
 productIDCol.HeaderText = "Product ID";
 productIDCol.ReadOnly = true;

 DataGridTextBoxColumn productNameCol = new DataGridTextBoxColumn( );
 productNameCol.MappingName = "ProductName";
 productNameCol.HeaderText = "Name";
 productNameCol.Width = 200;
 productNameCol.Alignment = HorizontalAlignment.Center;

 DataGridBoolColumn discontinuedCol = new DataGridBoolColumn( );
 discontinuedCol.MappingName = "Discontinued";
 discontinuedCol.HeaderText = "Discontinued";
 discontinuedCol.AllowNull = false;


 // Bind the default view of the table to the grid.
 dataGrid.DataSource = dt.DefaultView;

private void updateButton_Click(object sender, System.EventArgs e)
 // Update the data using the DataAdapter.


The TableStyles property of the DataGrid exposes a collection of DataGridTableStyle objects. The GridColumnStyles property of the DataGridTableStyle object exposes a GridColumnStylesCollection containing all DataGridColumnStyle objects for the table. This property allows a custom set of column styles to be created for the DataGrid . Once defined, the Add( ) method of the DataGridTableStyles object is used to add the custom column styles to the DataGrid .

The MappingName property of the DataGridTableStyle is set to the DataSource . The MappingName of each DataGridColumnStyle object must be associated with the name of a DataColumn to synchronize the DataGrid display column with the data column. An exception will be thrown if duplicate mapping names are used.

The DataGridTextBoxColumn class inherits from the abstract DataGridColumnStyle class. It defines the attributes, display format, and behavior of cells in a DataGrid column. At runtime, each cell in the column hosts a DataGridTextBox control.

The DataGridBoolColumn inherits from the abstract DataGridColumnStyle class. It defines the attributes, display format, and behavior of cells in a DataGrid column representing a Boolean value. At runtime, each cell in the column hosts a CheckBox control that can have one of three states: checked ( true ), unchecked ( false ), or unchecked ( DBNull.Value ). The allowable states of the check box are controlled by the properties described in Table 7-10.

Table 7-10. DataGridBoolColumn properties related to the underlying value




Gets or sets a Boolean value indicating whether null values are allowed


Gets or sets the value pushed to the data source when the column value is set to false


Gets or sets the value pushed to the data source when the column value is null


Gets or sets the value pushed to the data source when the column value is set to true

A new column class can be created to meet special requirements by inheriting from DataGridColumnStyle , where the Abort( ) , Commit( ) , Edit( ) , and Paint( ) methods must be overridden.

By default, a collection of DataGridColumnStyle objects is created behind the scenes when the DataSource property of the DataGrid is set. The class used for each column depends on the DataType of the DataColumn associated with the DataGridColumnStyle object. A column with a Boolean data type will be represented by a DataGridBoolColumn object while other columns will be represented by a DataGridTextBoxColumn object. You can use the GetType( ) method of the DataGridColumnStyle object to determine the column's data type.

Connecting to Data

Retrieving and Managing Data

Searching and Analyzing Data

Adding and Modifying Data

Copying and Transferring Data

Maintaining Database Integrity

Binding Data to .NET User Interfaces

Working with XML

Optimizing .NET Data Access

Enumerating and Maintaining Database Objects

Appendix A. Converting from C# to VB Syntax

show all menu

ADO. NET Cookbook
ADO.NET 3.5 Cookbook (Cookbooks (OReilly))
ISBN: 0596101406
EAN: 2147483647
Year: 2002
Pages: 222
Authors: Bill Hamilton
Similar book on Amazon © 2008-2017.
If you may any questions please contact us: