Tải bản đầy đủ (.pdf) (4 trang)

Creating Custom Columns in a Windows Forms DataGrid

Bạn đang xem bản rút gọn của tài liệu. Xem và tải ngay bản đầy đủ của tài liệu tại đây (16.99 KB, 4 trang )

[ Team LiB ]


Recipe 7.10 Creating Custom Columns in a Windows Forms DataGrid
Problem
You need to create a DataGrid having columns with custom formatting and attributes.
Solution
Use the DataGridTableStyle class.
The sample code contains two event handlers:
Form.Load
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 columns—two text boxes and one check box—
corresponding 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,
ConfigurationSettings.AppSettings["Sql_ConnectString"]);
// 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.
da.Fill(dt);

// 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;
ts.GridColumnStyles.Add(productIDCol);

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

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

dataGrid.TableStyles.Add(ts);

// 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.
da.Update(dt);
}
Discussion
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
Property Description
AllowNull Gets or sets a Boolean value indicating whether null values are allowed
FalseValue
Gets or sets the value pushed to the data source when the column value is
set to false
NullValue
Gets or sets the value pushed to the data source when the column value is
null
TrueValue
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.

[ Team LiB ]


×