資料繫結和 Windows FormData Binding and Windows Forms

在 Windows Forms 中,您不只可以繫結至傳統的資料來源,也能繫結至幾乎任何包含資料的結構。In Windows Forms, you can bind to not just traditional data sources, but also to almost any structure that contains data. 您可以繫結程序至執行階段計算、從檔案讀取,或衍生自其他控制項之值的值陣列。You can bind to an array of values that you calculate at run time, read from a file, or derive from the values of other controls.

此外,您也可以將任何控制項的任何屬性繫結至資料來源。In addition, you can bind any property of any control to the data source. 在傳統資料繫結中,您通常將顯示屬性 — 例如 Text 控制項的 TextBox 屬性 — 繫結到資料來源。In traditional data binding, you typically bind the display property—for example, the Text property of a TextBox control—to the data source. 使用.NET Framework 中,您也可以設定其他屬性,透過繫結的選擇。With the .NET Framework, you also have the option of setting other properties through binding as well. 您可以使用繫結來執行下列工作:You might use binding to perform the following tasks:

  • 設定影像控制項的圖形。Setting the graphic of an image control.

  • 設定一或多個控制項的背景色彩。Setting the background color of one or more controls.

  • 設定控制項的大小。Setting the size of controls.

基本上,資料繫結是在表單上自動設定任何控制項的任何執行階段可存取屬性的方法。Essentially, data binding is an automatic way of setting any run-time accessible property of any control on a form.

資料繫結的類型Types of Data Binding

Windows Forms 可以利用兩種類型的資料繫結:簡單繫結和複雜繫結。Windows Forms can take advantage of two types of data binding: simple binding and complex binding. 各有不同的優點。Each offers different advantages.

資料繫結的類型Type of data binding 描述Description
簡單資料繫結Simple data binding 控制項繫結至單一資料項目的能力,例如資料集中資料表的資料行值。The ability of a control to bind to a single data element, such as a value in a column in a dataset table. 這是一般適用於例如 TextBox 控制項或 Label 控制項等控制項的繫結類型,這些控制項通常只會顯示單一值。This is the type of binding typical for controls such as a TextBox control or Label control, which are controls that typically only displays a single value. 事實上,控制項上的任何屬性都可以繫結至資料庫中的欄位。In fact, any property on a control can be bound to a field in a database. 沒有這項功能在 Visual Studio 中的廣泛支援。There is extensive support for this feature in Visual Studio.

如需詳細資訊,請參閱:For more information, see:

- 與資料繫結相關的介面- Interfaces Related to Data Binding
- 如何:瀏覽 Windows Form 中的資料- How to: Navigate Data in Windows Forms
- 如何:建立 Windows Form 上的簡單繫結控制項- How to: Create a Simple-Bound Control on a Windows Form
複雜資料繫結Complex data binding 控制項繫結至一個以上資料項目的能力,在資料庫中通常會超過一筆記錄。The ability of a control to bind to more than one data element, typically more than one record in a database. 複雜繫結也稱為清單架構繫結。Complex binding is also called list-based binding. 支援複雜繫結的控制項範例有 DataGridViewListBoxComboBox 控制項。Examples of controls that support complex binding are the DataGridView, ListBox, and ComboBox controls. 如需複雜資料繫結的範例,請參閱How to:將 Windows Form ComboBox 或 ListBox 控制項繫結至資料For an example of complex data binding, see How to: Bind a Windows Forms ComboBox or ListBox Control to Data.

BindingSource 元件BindingSource Component

為了簡化資料繫結,Windows Forms 可讓您將資料來源繫結至 BindingSource 元件,然後將控制項繫結至 BindingSourceTo simplify data binding, Windows Forms enables you to bind a data source to the BindingSource component and then bind controls to the BindingSource. 您可以在簡單或複雜繫結案例中使用 BindingSourceYou can use the BindingSource in simple or complex binding scenarios. 在任一種情況下,BindingSource 擔任了資料來源與繫結程序控制項之間的媒介,提供變更通知流通管理和其他服務。In either case, the BindingSource acts as an intermediary between the data source and bound controls providing change notification currency management and other services.

使用資料繫結的常見案例Common Scenarios That Employ Data Binding

幾乎所有商業應用程式都使用讀取自某種資料來源的資訊,通常是透過資料繫結。Nearly every commercial application uses information read from data sources of one type or another, usually through data binding. 下列清單顯示一些最常見的案例,利用資料繫結作為資料展示和管理的方法。The following list shows a few of the most common scenarios that utilize data binding as the method of data presentation and manipulation.

情節Scenario 描述Description
報告Reporting 報表會提供有彈性的方式,以在書面文件中顯示和彙總資料。Reports provide a flexible way for you to display and summarize your data in a printed document. 建立報表,將選取的資料來源內容列印至螢幕或印表機,是很常見的作法。It is very common to create a report that prints selected contents of a data source either to the screen or to a printer. 常見的報表包含清單、發票和摘要。Common reports include lists, invoices, and summaries. 項目通常會格式化成清單資料行,且將子項目組織在每個清單項目下,但您應該選擇最適合資料的版面配置。Items are usually formatted into columns of lists, with sub-items organized under each list item, but you should choose the layout that best suits the data.
資料輸入Data entry 若要輸入大量相關資料,或提示使用者輸入資訊,常用的方式是透過資料輸入表單。A common way to enter large amounts of related data or to prompt users for information is through a data entry form. 使用者可以輸入資訊,或使用文字方塊、選項按鈕、下拉式清單和核取方塊來選取選項。Users can enter information or select choices using text boxes, option buttons, drop-down lists, and check boxes. 資訊便會送出並儲存在資料庫中,其結構是根據輸入的資訊。Information is then submitted and stored in a database, whose structure is based on the information entered.
主從式關係Master/detail relationship 主從式應用程式是查看相關資料的一種格式。A master/detail application is one format for looking at related data. 明確地說,有兩個具有相連關係的資料表 — 在一般的商業範例中,「客戶」資料表和「訂單」資料表,它們之間連結了客戶和個別訂單的關係。Specifically, there are two tables of data with a relation connecting them—in the classic business example, a "Customers" table and an "Orders" table with a relationship between them linking customers and their respective orders. 如需有關建立主從式應用程式使用兩個 Windows FormDataGridView控制項,請參閱How to:建立主版/詳細表單使用兩個 Windows Form DataGridView 控制項For more information about creating a master/detail application with two Windows Forms DataGridView controls, see How to: Create a Master/Detail Form Using Two Windows Forms DataGridView Controls
查閱資料表Lookup Table 另一個常見的資料展示/管理案例是資料表查閱。Another common data presentation/manipulation scenario is the table lookup. 通常,在較大的資料顯示中,ComboBox 控制項是用來顯示和管理資料。Often, as part of a larger data display, a ComboBox control is used to display and manipulate data. 關鍵在於 ComboBox 控制項中顯示的資料與寫入資料庫的資料不同。The key is that the data displayed in the ComboBox control is different than the data written to the database. 例如,如果您有 ComboBox 控制項,顯示可從雜貨店取得的項目,您可能會想看到產品的名稱 (麵包、牛奶、蛋)。For example, if you have a ComboBox control displaying the items available from a grocery store, you would probably like to see the names of the products (bread, milk, eggs). 不過,為了簡化資料庫中的資訊擷取以及資料庫的正規化,您可能會將特定訂單的特定項目資訊儲存為項目編號 (#501、#603 等)。However, to ease information retrieval within the database and for database normalization, you would probably store the information for the specific items of a given order as item numbers (#501, #603, and so on). 因此,表單上 ComboBox 控制項中的雜貨項目的「易記名稱」,與訂單中出現的相關項目編號,兩者之間有隱含的連接。Thus, there is an implicit connection between the "friendly name" of the grocery item in the ComboBox control on your form and the related item number that is present in an order. 這是資料表查閱的本質。This is the essence of a table lookup. 如需詳細資訊,請參閱如何:使用 Windows Forms BindingSource 元件建立查閱資料表For more information, see How to: Create a Lookup Table with the Windows Forms BindingSource Component.

另請參閱See also