WinForm 2.0 下拉框的绑定
来源:互联网 发布:矩阵式项目管理组织 编辑:程序博客网 时间:2024/06/18 11:53
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
![](mk:@MSITStore:D:/Documents%20and%20Settings/Administrator/桌面/Addison.Wesley.Data.Binding.with.Windows.Forms.2.0.Programming.Smart.Client.Data.Applications.with.dot.NET.Jan.2006.chm::/032126892X/images/03fig02.jpg)
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.
- WinForm 2.0 下拉框的绑定
- winForm中绑定ComboBox下拉框
- C#-WinForm班级下拉框数据绑定
- web下拉框动态绑定数据库 winform固定绑定
- C#--WinForm项目核心模块 --用DataTable绑定下拉框的数据源
- select下拉框的绑定
- 下拉框的绑定<select>
- winform下拉框篇.类似Word的颜色下拉框
- winform自定义下拉框
- # winform comboBox动态下拉列表,comboBox动态绑定数据库里的数据
- 绑定树形结构的下拉框
- DataGridView里的下拉框绑定
- Bootstrap 下拉框的绑定方法
- gridview绑定下拉框
- ultraWinGrid绑定下拉框
- Ext 绑定下拉框
- 绑定下拉框
- 绑定下拉框数据
- IE6 对浮动元素内的未指定宽度的块级元素错误的解释 BUG
- WinForm 2.0 有代码两个DataGridView实现Master/Details
- nutch-0.8在eclipse中运行
- WinForm 2.0 textBox作为参数的查询,显示在DataGridView
- asp.net中正则表达式常用说明
- WinForm 2.0 下拉框的绑定
- WinForm 2.0 无代码DataGridView绑定
- WinForm 2.0 应用一 无代码实现详细信息绑定
- Iterator源码剖析
- 正则表达式-ASP.NET
- 理解性能测试(一)
- PSP开发英雄在索尼恐吓下黯然离去
- .net 函数大全
- java面试笔试题整理