DataGridView 控件概述(Windows 窗体)DataGridView Control Overview (Windows Forms)

备注

DataGridView 控件取代了 DataGrid 控件并添加了功能;但是,可以选择保留 DataGrid 控件以实现向后兼容并供将来使用。The DataGridView control replaces and adds functionality to the DataGrid control; however, the DataGrid control is retained for both backward compatibility and future use, if you choose. 有关详细信息,请参阅 Windows 窗体 DataGridView 控件与 DataGrid 控件之间的区别For more information, see Differences Between the Windows Forms DataGridView and DataGrid Controls.

利用 DataGridView 控件,可以显示和编辑多种不同类型的数据源中的表格数据。With the DataGridView control, you can display and edit tabular data from many different kinds of data sources.

将数据绑定到 DataGridView 控件非常简单直观,在许多情况下,只需设置 DataSource 属性即可。Binding data to the DataGridView control is straightforward and intuitive, and in many cases it is as simple as setting the DataSource property. 绑定到包含多个列表或表的数据源时,请将 DataMember 属性设置为一个字符串,该字符串指定要绑定到的列表或表。When you bind to a data source that contains multiple lists or tables, set the DataMember property to a string that specifies the list or table to bind to.

DataGridView 控件支持标准 Windows 窗体数据绑定模型,因此它将绑定到以下列表中所述的类的实例:The DataGridView control supports the standard Windows Forms data binding model, so it will bind to instances of classes described in the following list:

如果在返回的对象上实现,则 DataGridView 控件支持数据绑定到由这些接口返回的对象的公共属性或由 ICustomTypeDescriptor 接口返回的 properties 集合。The DataGridView control supports data binding to the public properties of the objects returned by these interfaces or to the properties collection returned by an ICustomTypeDescriptor interface, if implemented on the returned objects.

通常,你将绑定到 BindingSource 组件,并将 BindingSource 组件绑定到另一个数据源,或者使用业务对象来填充它。Typically, you will bind to a BindingSource component and bind the BindingSource component to another data source or populate it with business objects. BindingSource 组件是首选数据源,因为它可以绑定到各种数据源,并可自动解决许多数据绑定问题。The BindingSource component is the preferred data source because it can bind to a wide variety of data sources and can resolve many data binding issues automatically. 有关详细信息,请参阅BindingSource ComponentFor more information, see BindingSource Component.

DataGridView 控件还可用于未绑定模式,无基础数据存储。The DataGridView control can also be used in unbound mode, with no underlying data store. 有关使用未绑定 DataGridView 控件的代码示例,请参阅演练:创建未绑定的 Windows 窗体 DataGridView 控件For a code example that uses an unbound DataGridView control, see Walkthrough: Creating an Unbound Windows Forms DataGridView Control.

DataGridView 控件高度可配置且可扩展,并且提供了许多属性、方法和事件来自定义其外观和行为。The DataGridView control is highly configurable and extensible, and it provides many properties, methods, and events to customize its appearance and behavior. 如果希望 Windows 窗体应用程序显示表格数据,请考虑使用其他 DataGridView 控件(例如 DataGrid)。When you want your Windows Forms application to display tabular data, consider using the DataGridView control before others (for example, DataGrid). 如果要显示只读值的小网格,或者,如果您要使用户能够编辑包含数百万条记录的表,则 DataGridView 控件将为您提供一个易于编程的、内存有效的解决方案。If you are displaying a small grid of read-only values, or if you are enabling a user to edit a table with millions of records, the DataGridView control will provide you with a readily programmable, memory-efficient solution.

本节内容In This Section

DataGridView 控件技术摘要DataGridView Control Technology Summary
概述 DataGridView 控件概念以及相关类的用法。Summarizes DataGridView control concepts and the use of related classes.

DataGridView 控件体系结构DataGridView Control Architecture
描述 DataGridView 控件的体系结构,说明其类型层次结构和继承结构。Describes the architecture of the DataGridView control, explaining its type hierarchy and inheritance structure.

DataGridView 控件应用场景DataGridView Control Scenarios
描述使用 DataGridView 控件的最常见方案。Describes the most common scenarios in which DataGridView controls are used.

DataGridView 控件代码目录DataGridView Control Code Directory
提供有关各种 DataGridView 任务的文档中的代码示例的链接。Provides links to code examples in the documentation for various DataGridView tasks. 这些示例按任务类型进行分类。These examples are categorized by task type.

Windows 窗体 DataGridView 控件中的列类型Column Types in the Windows Forms DataGridView Control
讨论用于显示信息以及允许用户修改或添加信息的 Windows 窗体 DataGridView 控件中的列类型。Discusses the column types in the Windows Forms DataGridView control used to display information and allow users to modify or add information.

在 Windows 窗体 DataGridView 控件中显示数据Displaying Data in the Windows Forms DataGridView Control
提供一些主题,描述如何手动或从外部数据源向控件填充数据。Provides topics that describe how to populate the control with data either manually, or from an external data source.

自定义 Windows 窗体 DataGridView 控件Customizing the Windows Forms DataGridView Control
提供一些主题,介绍自定义绘制 DataGridView 单元格和行,以及创建派生的单元、列和行类型。Provides topics that describe custom painting DataGridView cells and rows, and creating derived cell, column, and row types.

Windows 窗体 DataGridView 控件中的性能调整Performance Tuning in the Windows Forms DataGridView Control
提供一些主题,介绍如何在使用大量数据时,有效地使用控件以避免性能问题。Provides topics that describe how to use the control efficiently to avoid performance problems when working with large amounts of data.

另请参阅See also