DataGridView 控制項概觀 (Windows Form)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 Forms 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 控制項既簡單又直覺,而且在許多情況下,只要設定屬性就可以了 DataSourceBinding 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 Forms 資料系結模型,因此它會系結至下列清單中所述的類別實例: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 (如果在傳回的物件上執行的話)。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 元件For more information, see BindingSource Component.

DataGridView控制項也可以在 系結模式中使用,而不會有基礎資料存放區。The DataGridView control can also be used in unbound mode, with no underlying data store. 如需使用未系結控制項的程式碼範例 DataGridView ,請參閱 逐步解說:建立未系結的 Windows Forms 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 Forms 應用程式顯示表格式資料時,請考慮在 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
描述使用控制項的最常見案例 DataGridViewDescribes the most common scenarios in which DataGridView controls are used.

DataGridView 控制項程式碼目錄DataGridView Control Code Directory
提供檔中各種工作的程式碼範例連結 DataGridViewProvides links to code examples in the documentation for various DataGridView tasks. 這些範例是以工作類型分類。These examples are categorized by task type.

Windows Form DataGridView 控制項中的資料行類型Column Types in the Windows Forms DataGridView Control
討論 Windows Forms DataGridView 控制項中用來顯示資訊,並允許使用者修改或新增資訊的資料行類型。Discusses the column types in the Windows Forms DataGridView control used to display information and allow users to modify or add information.

在 Windows Form 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 Form 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 Form 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