22.1.3 DataGrid类的层次结构
DataGrid主要部分的类层次结构如图22-10所示。

图 22-10
DataGrid由0个或多个DataGridTableStyles组成。样式风格包含0个或多个DataGrid Column Styles。网格中给定的单元格可以通过DataGridCell结构来访问。
但除了让运行程序创建DataGridTables 和 DataGridColumns外,DataGridTableStyles 和 DataGridColumnStyles还有更多的内涵。下面的几节就详细介绍这两个类和上图中列出的其 他类。
下面介绍DataGridTableStyle和 DataGridColumnStyle的内容。
DataGridTableStyle包含DataTable的可视化表示。DataGrid包含这些样式的一个集合,它们可通过TableStyles属性来访问。在显示DataTable时,要对所有的DataGridTableStyle对象进行检查,以查找MappingName等于 DataTable的TableName属性的样式匹配,找到这样的一个匹配后,就使用它显示数据表。
DataGridTableStyle允许为DataGrid定义各种可视化参数,例如背景色和前景色,列标题的字体以及其他属性。DataGridColumnStyle可以一列一列地细调显示选项,例如设置列中数据的对齐方式,NULL值的显示文本,列在屏幕上的宽度等。
在DataGrid使用一个已定义好的DataGridTableStyle来显示DataTable时,只会显示那些已构建了DataGridColumnStyle的列。只有定义了风格的列才能显示,这非常适合于隐藏某些列,例如主键码值一般不显示,也可以把列的样式定义为ReadOnly。列的隐藏不像过滤列和过滤行的方法那样简单,但也不是很难。
下面的代码显示了创建DataGridTableStyle的示例,这个示例创建了一个DataGridTableStyle对象,添加两个DataColumnStyle对象,然后显示Customers表中的所有数据,下面列出了所有的代码,因为它是本章其他几个示例的基础。代码的第一部分与前面示例的类似:
using System;
using System.Windows.Forms;
using System.Data;
using System.Data.SqlClient;
public class CustomDataGridTableStyle : System.Windows.Forms.Form
{
private System.Windows.Forms.Button retrieveButton;
private System.Windows.Forms.DataGrid dataGrid;
public CustomDataGridTableStyle()
{
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(464, 253);
this.Text = "07_CustomDataGridTableStyle";
this.dataGrid = new System.Windows.Forms.DataGrid();
dataGrid.BeginInit();
dataGrid.Location = new System.Drawing.Point(8, 8);
dataGrid.Size = new System.Drawing.Size(448, 208);
dataGrid.TabIndex = 0;
dataGrid.Anchor = AnchorStyles.Bottom | AnchorStyles.Top |
AnchorStyles.Left | AnchorStyles.Right;
this.Controls.Add(this.dataGrid);
dataGrid.EndInit();
this.retrieveButton = new System.Windows.Forms.Button();
retrieveButton.Location = new System.Drawing.Point(384, 224);
retrieveButton.Size = new System.Drawing.Size(75, 23);
retrieveButton.TabIndex = 1;
retrieveButton.Anchor = AnchorStyles.Bottom | AnchorStyles.Right;
retrieveButton.Text = "Retrieve";
retrieveButton.Click += new
System.EventHandler(this.retrieveButton_Click);
this.Controls.Add(this.retrieveButton);
}
protected void retrieveButton_Click(object sender, System.EventArgs e)
{
retrieveButton.Enabled = false;
这些代码生成将要使用的DataSet,接着创建了本例中要使用的DataGridTableStyle,最后把DataGrid绑定到DataSet上。CreateDataSet函数并不是新东西,它可以从Customers表中选择出所有的行。
DataSet ds = CreateDataSet();
CreateStyles(dataGrid);
dataGrid.SetDataBinding(ds, "Customers");
}
CreateStyles()方法比较有趣。前几行代码创建新的DataGridTableStyle对象,并且设置了它的MappingName属性,这个属性在DataGrid显示一个给定的DataTable时使用。DataGrid可以用交替变化的颜色显示数据行。这段代码还每隔一行定义数据行的颜色(结果如图22-11所示):
private void CreateStyles(DataGrid dg)
{
DataGridTableStyle style = new DataGridTableStyle();
style.MappingName = "Customers";
style.AlternatingBackColor = System.Drawing.Color.Bisque;
DataGridTextBoxColumn customerID = new DataGridTextBoxColumn();
customerID.HeaderText = "Customer ID";
customerID.MappingName = "CustomerID";
customerID.Width = 200;
DataGridTextBoxColumn name = new DataGridTextBoxColumn();
name.HeaderText = "Name";
name.MappingName = "CompanyName";
name.Width = 300;
在定义了数据列后,就把它们添加到DataGridTableStyle对象的GridColumnStyles集合中,该对象本身则被添加到DataGrid 的属性TableStyles上:
style.GridColumnStyles.AddRange
(new DataGridColumnStyle[]{customerID , name});
dg.TableStyles.Add(style);
}
private DataSet CreateDataSet()
{
string source = "server=(local)\\NetSDK;" +
"uid=QSUser;pwd=QSPassword;" +
"database=northwind";
string customers = "SELECT * FROM Customers";
SqlConnection con = new SqlConnection(source);
SqlDataAdapter da = new SqlDataAdapter(customers , con);
DataSet ds = new DataSet();
da.Fill(ds, "Customers");
return ds;
}
static void Main()
{
Application.Run(new CustomDataGridTableStyle());
}
}
在创建了DataGridTableStyle对象后,就创建了两个派生于DataGridColumnStyle的对象,在本实例中,是两个文本框。每一列都定义了许多属性,见表22-3。
表 22-3
|
属 性 |
说 明 |
|
Alignment |
HorizontalAlignment枚举的一个值—— Left、 Center或 Right,这表示列中数据的对齐方式 |
|
FontHeight |
字体的大小(像素),如果没有给它设置值,就使用DataGrid的默认值。这个属性是受保护的,只有在创建自己的子类时,才能修改它 |
|
HeaderText |
显示在列标题中的文本 |
|
MappingName |
在屏幕上显示的DataTable的列 |
|
NullText |
底层的数据值为DBNull时在列中显示的文本 |
|
PropertyDescriptor |
本章后面讨论 |
|
ReadOnly |
标志,表示列是可读写的,还是只读的 |
|
Width |
列的宽度(像素) |
这个CustomDataGridTableStyle示例的显示结果如图22-11所示。

图 22-11





