(轉)DataGridView 控件结构(Windows 窗体)

来源:互联网 发布:vs上mpi编程 编辑:程序博客网 时间:2024/05/09 11:18
本文轉自:http://msdn2.microsoft.com/zh-cn/library/dkdz8z4e(VS.80).aspx
DataGridView 控件结构(Windows 窗体)

 

DataGridView 控件及其相关类旨在提供一个灵活的可扩展系统,用于显示和编辑表格数据。这些类全部包含在 System.Windows.Forms 命名空间中,并全部以“DataGridView”前缀命名。

结构元素

主要的 DataGridView 伴生类从 DataGridViewElement 派生。下面的对象模型演示了 DataGridViewElement 继承层次结构。

 

DataGridViewElement 对象模型

 


DataGridViewElement 对象模型

DataGridViewElement 类提供对 DataGridView 父控件的引用,并具有一个 State 属性,该属性所包含的值由 DataGridViewElementStates 枚举的值组合而成。

以下各节对 DataGridView 伴生类进行了更详细地介绍。

DataGridViewElementStates

DataGridViewElementStates 枚举包含下列值:

  • None

  • Frozen

  • ReadOnly

  • Resizable

  • ResizableSet

  • Selected

  • Visible

可以使用按位逻辑运算符对此枚举的值进行组合,因此 State 属性一次可以表示多个状态。例如,DataGridViewElement 可以同时表示 FrozenSelectedVisible

单元格和带区

DataGridView 控件由两种基本类型的对象组成:单元格和带区。所有单元格都是从 DataGridViewCell 基类派生的。两种类型的带区(DataGridViewColumn 和 DataGridViewRow)都是从 DataGridViewBand 基类派生的。

DataGridView 控件可以与多个类进行互操作,但最常用的类为 DataGridViewCellDataGridViewColumnDataGridViewRow

DataGridViewCell

单元格是 DataGridView 的基本交互单元。单元格中的内容居中显示,并且通常通过单元格来输入数据。通过使用 DataGridViewRow 类的 Cells 集合可以访问单元格,通过使用 DataGridView 控件的 SelectedCells 集合可以访问选定的单元格。下面的对象模型演示了此用法,并显示 DataGridViewCell 继承层次结构。

 

DataGridViewCell 对象模型

 


DataGridViewCell 对象模型

DataGridViewCell 类型是一种抽象基类,所有单元格类型都是从该类派生的。DataGridViewCell 及其派生类型不是 Windows 窗体控件,但有些类型可以承载 Windows 窗体控件。单元格所支持的任何编辑功能通常都由被承载的控件进行处理。

DataGridViewCell 对象不能像 Windows 窗体控件那样控制自己的外观和绘制功能,而是由 DataGridView 负责其 DataGridViewCell 对象的外观。通过与 DataGridView 控件的属性和事件进行交互操作,可以显著影响单元格的外观和行为。如果您有超出 DataGridView 控件的功能之外的特殊自定义需求,可以实现自己的从 DataGridViewCell 或其某个子类派生的类。

下表显示从 DataGridViewCell 派生的类:

  • DataGridViewTextBoxCell

  • DataGridViewButtonCell

  • DataGridViewLinkCell

  • DataGridViewCheckBoxCell

  • DataGridViewComboBoxCell

  • DataGridViewImageCell

  • DataGridViewHeaderCell

  • DataGridViewRowHeaderCell

  • DataGridViewColumnHeaderCell

  • DataGridViewTopLeftHeaderCell

  • 自定义单元格类型

DataGridViewColumn

DataGridView 控件的附加数据存储区的架构通过 DataGridView 控件的列来表示。可以使用 Columns 集合访问 DataGridView 控件的列。可以使用 SelectedColumns 集合访问选定的列。下面的对象模型演示了此用法,并显示 DataGridViewColumn 继承层次结构。

 

DataGridViewColumn 对象模型

 


DataGridViewColumn 对象模型

一些主要的单元格类型具有相应的列类型。这些列类型是从 DataGridViewColumn 基类派生的。

下表显示从 DataGridViewColumn 派生的类:

  • DataGridViewButtonColumn

  • DataGridViewCheckBoxColumn

  • DataGridViewComboBoxColumn

  • DataGridViewImageColumn

  • DataGridViewTextBoxColumn

  • DataGridViewLinkColumn

  • 自定义列类型

DataGridView 编辑控件

支持高级编辑功能的单元格通常使用从 Windows 窗体控件派生的寄宿的控件。这些控件还实现 IDataGridViewEditingControl 接口。下面的对象模型演示了这些控件的用法。

 

DataGridView 编辑控件对象模型

 


DataGridView 编辑控件对象模型

DataGridView 控件一起提供了下列编辑控件:

  • DataGridViewComboBoxEditingControl

  • DataGridViewTextBoxEditingControl

有关创建自己的编辑控件的信息,请参见如何:在 Windows 窗体 DataGridView 单元格中承载控件。

下表阐释了单元格类型、列类型和编辑控件之间的关系。

单元格类型 寄宿的控件 列类型

DataGridViewButtonCell

n/a

DataGridViewButtonColumn

DataGridViewCheckBoxCell

n/a

DataGridViewCheckBoxColumn

DataGridViewComboBoxCell

DataGridViewComboBoxEditingControl

DataGridViewComboBoxColumn

DataGridViewImageCell

n/a

DataGridViewImageColumn

DataGridViewLinkCell

n/a

DataGridViewLinkColumn

DataGridViewTextBoxCell

DataGridViewTextBoxEditingControl

DataGridViewTextBoxColumn

DataGridViewRow

DataGridViewRow 类显示 DataGridView 控件附加到的数据存储区中的某记录的数据字段。通过使用 Rows 集合可以访问 DataGridView 控件的行。通过使用 SelectedRows 集合可以访问选定的行。下面的对象模型演示了此用法,并显示 DataGridViewRow 继承层次结构。

 

DataGridViewRow 对象模型

 


DataGridViewRow 对象模型

您可以从 DataGridViewRow 类派生自己的类型,尽管通常无需这么做。DataGridView 控件具有一些与行相关的事件和属性,可以用于自定义其 DataGridViewRow 对象的行为。

如果启用 DataGridView 控件的 AllowUserToAddRows 属性,则将会在最后一行显示一个用于添加新行的特殊行。此行是 Rows 集合的一部分,但您可能需要关注其所具有的特殊功能。有关更多信息,请参见在 Windows 窗体 DataGridView 控件中使用新记录行。

请参见

参考

DataGridView 控件概述(Windows 窗体)

概念

在 Windows 窗体 DataGridView 控件中使用新记录行

其他资源

自定义 Windows 窗体 DataGridView 控件

 
原创粉丝点击