WinForm 2.0 下拉框的绑定

本博客已搬家 地址:www.czhphp.com 所有更新都会在新博客进行 谢谢大家的支持!

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
[View full size image]

 

 

 

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.

本博客已搬家 地址:www.czhphp.com 所有更新都会在新博客进行 谢谢大家的支持!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
WinForm下拉框是通过ComboBox控件实现的。在WinForm中,可以使用ComboBox控件来创建下拉框。ComboBox控件是继承自System.Windows.Forms命名空间下的ComboBox类的自定义控件。\[2\] 当选择下拉菜单中的选项时,可以通过combobox_SelectedIndexChanged事件来执行相应的代码。在该事件中,可以使用SelectedItem属性来获取当前选中的选项的值,使用SelectedIndex属性来获取当前选中的选项的索引。例如,可以使用MessageBox.Show方法来显示当前选中的选项的值和索引。\[1\] 下拉框是一种常用的用户界面控件,它可以让用户从预定义的选项中选择一个值。在WinForm中,可以通过ComboBox控件来创建下拉框,并通过combobox_SelectedIndexChanged事件来处理选项的选择。 #### 引用[.reference_title] - *1* [WinForm学习03 下拉菜单](https://blog.csdn.net/weixin_47326735/article/details/117201389)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control,239^v3^insert_chatgpt"}} ] [.reference_item] - *2* [winform 下拉框(自定义下拉框控件)](https://blog.csdn.net/pyc_01/article/details/106626643)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值