组合框textChanged事件未触发

本文关键字:事件 textChanged 组合 | 更新日期: 2023-09-27 18:14:42

我们有包含Cust_ID, Cust_Name等的自定义表.....对于这个表,Cust_Name不是唯一的,一个客户名可以重复的次数。

我正在获取数据从SQL和绑定到ComboBox (winform)

 cmbCustomar.Datasource = GetCustomerData(_LocationID);
 cmbCustomar.DisplayMember = "Cust_Name";
 cmbCustomar.ValueMember = "Cust_ID";

问题是:

Customer Name: JOHN重复4次,所有Cust_ID都不相同当用户在第一项上选择JOHN时,我得到正确的"SelectedValue"

,但如果用户选择第2或第3个JOHN组合框项目总是默认选择第一项(名称为JOHN)而SelectedValue总是返回第一项的值。

我找不到我做错的地方,请建议

组合框textChanged事件未触发

尝试更改以下属性:

cmbCustomar.DropDownStyle = DropDownList;

如果你的ComboBox有DropDownStyle = DropDown,那么ComboBox的"text"部分会尝试匹配它在列表中可以找到的第一个项目,在这种情况下,它会忽略当前选中的项目,并在列表中找到第一个"John"。

记住"SelectedValueChanged"事件将在组合框被填充时触发。请确保在填充组合框之前取消订阅此事件。并在填充数据后再次订阅。

            //unsubsribe the event before populating combobox1
        this.cmbCustomar.SelectedValueChanged -= new System.EventHandler(this.cmbCustomar_SelectedValueChanged);
        cmbCustomar.Datasource = GetCustomerData(_LocationID);
        cmbCustomar.DisplayMember = "Cust_Name";
        cmbCustomar.ValueMember = "Cust_ID";
        //subscribe the event again 
        this.cmbCustomar.SelectedValueChanged += new System.EventHandler(this.cmbCustomar_SelectedValueChanged);

看来你对它很陌生。根据我的猜测,由于您的描述不完整,您正在尝试根据组合框的选定文本返回"选定值"。但是,您必须尝试将值a附加到所选文本并返回该值。它一定能解决你的问题。

希望能有所帮助。