现在的位置: 首页 > 综合 > 正文

WinForm 2.0 下拉框的绑定

2012年12月08日 ⁄ 综合 ⁄ 共 3080字 ⁄ 字号 评论关闭

Binding Data Collections to Multi-Valued Controls

Another common scenario is to populate a list box or combo box from a property within a collection of data. Listing 3.1 showed an example of this. The code to bind a combo box or list box isn't all that different from the code to bind a collection of data to a grid. The difference lies in how you specify which property within the collection to use for presentation in the control (the DisplayMember), and which property to hold onto for each item as an associated value (the ValueMember).

The value of the property set as the ValueMember for the current item in the combo box or list box is accessible through the SelectedValue property on the control at runtime. For example, take a look at the form in Figure 3.2.

Figure 3.2. ComboBox SelectedValue Sample

 

 

If you hooked up the combo boxes and text boxes on this form as shown in Listing 3.3, the SelectedValue property on the combo boxes are used to drive the contents of the text boxes.

Listing 3.3. Using SelectedValue from a Data-Bound ComboBox

public partial class SelectedValueForm : Form
            {
            public SelectedValueForm()
            {
            InitializeComponent();
            }
            private void OnFormLoad(object sender, EventArgs e)
            {
            // Get the data
            m_CustomersDataSet = CustomersDataSet.GetCustomers();
            // Set up the bindings for the combo boxes
            m_CompanyNameCombo.DataSource = m_CustomersDataSet.Customers;
            m_CompanyNameCombo.DisplayMember = "CompanyName";
            m_CompanyNameCombo.ValueMember = "CustomerID";
            m_ContactNameCombo.DataSource = m_CustomersDataSet.Customers;
            m_ContactNameCombo.DisplayMember = "ContactName";
            m_ContactNameCombo.ValueMember = "Phone";
            // Hook up event handlers for selection change events
            m_CompanyNameCombo.SelectedIndexChanged
            += OnComboSelectionChanged;
            }
            private void OnComboSelectionChanged(object sender, EventArgs args)
            {
            if (m_ContactNameCombo.SelectedValue == null ||
            m_CompanyNameCombo.SelectedValue == null)
            return;
            m_CustomerIDTextBox.Text =
            m_CompanyNameCombo.SelectedValue.ToString();
            m_ContactPhoneTextBox.Text =
            m_ContactNameCombo.SelectedValue.ToString();
            }
            }

In this example, the combo boxes are bound to the Customers table in the typed data set. The DisplayMember for each will show the CompanyName and ContactName properties (columns), respectively, as the text in the combo box for each item in the Customers table. Additionally, the ValueMember for the first combo box is set to the CustomerID property, and the second combo box is set to the Phone property. By doing this, the SelectedValue property on the combo box will give you back the value of the ValueMember property on the currently selected item in the combo box whenever it is called. In the code in Listing 3.3, the SelectedValue property for each of the combo boxes is accessed in an event handler for the SelectedIndexChanged event on the combo box, and it is used to set the Text property on the corresponding text boxes.

This same functionality could have been achieved better by simply data binding the text boxes as well, but the intent in this code was to demonstrate using the SelectedValue property and to show how it relates to the ValueMember. Instead of using the SelectedValue to populate a text box, a more common usage would be to perform a lookup query against the database for the CustomerID contained in the SelectedValue for the first combo box. You would typically do something like this if you have a large data set that you don't want to hold in memory just to display a single column in a combo box or list box, but you want to get the full row corresponding to a selection to act on it in your code.

抱歉!评论已关闭.