如何:为 Windows 窗体 ComboBox 控件、ListBox 控件或 CheckedListBox 控件创建查找表How to: Create a Lookup Table for a Windows Forms ComboBox, ListBox, or CheckedListBox Control

有时,在 Windows 窗体上以用户友好格式显示数据,但存储数据时使用对程序而言更有意义的格式会很有帮助。Sometimes it is useful to display data in a user-friendly format on a Windows Form, but store the data in a format that is more meaningful to your program. 例如,食品订单窗体可能按列表框中的名称显示菜单项。For example, an order form for food might display the menu items by name in a list box. 但是,记录订单的数据表将包含代表该食品的唯一 ID 号。However, the data table recording the order would contain the unique ID numbers representing the food. 下表显示如何存储和显示食品订单窗体数据的示例。The following tables show an example of how to store and display order-form data for food.

OrderDetailsTableOrderDetailsTable

OrderIDOrderID ItemIDItemID QuantityQuantity
40854085 1212 11
40864086 1313 33

ItemTableItemTable

IdID 名称Name
1212 PotatoPotato
1313 ChickenChicken

在此方案中,一个表中, OrderDetailsTable,存储您所关注显示和保存的实际信息。In this scenario, one table, OrderDetailsTable, stores the actual information you are concerned with displaying and saving. 但为了节省空间,它采用一种很隐秘的方式。But to save space, it does so in a fairly cryptic fashion. 另一个表ItemTable,包含有关哪个 ID 号等同于哪个食品名称,并执行任何操作有关的实际食品订单仅与外观相关的信息。The other table, ItemTable, contains only appearance-related information about which ID number is equivalent to which food name, and nothing about the actual food orders.

ItemTable连接到ComboBoxListBox,或CheckedListBox控制通过三个属性。The ItemTable is connected to the ComboBox, ListBox, or CheckedListBox control through three properties. DataSource属性包含此表的名称。The DataSource property contains the name of this table. DisplayMember属性包含你想要在控件 (食品名称) 中显示的表的数据列。The DisplayMember property contains the data column of that table that you want to display in the control (the food name). ValueMember属性包含的数据列的表中具有存储信息 (ID 号)。The ValueMember property contains the data column of that table with the stored information (the ID number).

OrderDetailsTable连接到控件通过其绑定集合,通过访问DataBindings属性。The OrderDetailsTable is connected to the control by its bindings collection, accessed through the DataBindings property. 时绑定对象添加到集合时,连接到数据源中的特定数据成员 (ID 号的列) 的控件属性 ( OrderDetailsTable)。When you add a binding object to the collection, you connect a control property to a specific data member (the column of ID numbers) in a data source (the OrderDetailsTable). 在控件中进行选择时,窗体输入将保存到此表。When a selection is made in the control, this table is where the form input is saved.

创建查找表的步骤To create a lookup table

  1. 向窗体添加 ComboBoxListBoxCheckedListBox 控件。Add a ComboBox, ListBox, or CheckedListBox control to the form.

  2. 连接到数据源。Connect to your data source.

  3. 在两张表之间建立数据关系。Establish a data relation between the two tables. 请参阅DataRelation 对象介绍See Introduction to DataRelation Objects.

  4. 设置以下属性:Set the following properties. 以下属性可在代码或设计器中设置。They can be set in code or in the designer.

    属性Property 设置Setting
    DataSource 包含有关哪个 ID 号等同于哪一项的信息的表。The table that contains information about which ID number is equivalent to which item. 在前面的方案,这是ItemTableIn the previous scenario, this is ItemTable.
    DisplayMember 你想要在控件中显示的源数据表的列。The column of the data source table that you want to display in the control. 在前面的方案,这是"Name"(若要在代码中设置,使用引号)。In the previous scenario, this is "Name" (to set in code, use quotation marks).
    ValueMember 包含所存储信息的源数据表的列。The column of the data source table that contains the stored information. 在前面的方案,这是"ID"(若要在代码中设置,使用引号)。In the previous scenario, this is "ID" (to set in code, use quotation marks).
  5. 在过程中,调用 ControlBindingsCollection 类的 Add 方法,以将控件的 SelectedValue 属性绑定到记录窗体输入的表。In a procedure, call the Add method of the ControlBindingsCollection class to bind the control's SelectedValue property to the table recording the form input. 您可以执行此操作而不是在代码中,在设计器中访问控件的DataBindings中的属性属性窗口。You can also do this in the Designer instead of in code, by accessing the control's DataBindings property in the Properties window. 在前面的方案,这是OrderDetailsTable,且列是"ItemID"In the previous scenario, this is OrderDetailsTable, and the column is "ItemID".

    ListBox1.DataBindings.Add("SelectedValue", OrderDetailsTable, "ItemID")  
    
    listBox1.DataBindings.Add("SelectedValue", OrderDetailsTable, "ItemID");  
    

请参阅See also