简体   繁体   English

组合框textChanged事件未触发

[英]Combobox textChanged event not firing

We have customar table contains Cust_ID, Cust_Name etc..... for this table Cust_Name is not unique and one Customer name can repeat Number of times. 我们的定制表包含Cust_ID,Cust_Name等.....对于此表,Cust_Name不是唯一的,并且一个客户名称可以重复次数。

i am getting data from SQL and binding to ComboBox (winform) 我正在从SQL获取数据并绑定到ComboBox(winform)

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

Here the Problem is : 这里的问题是:

Customer Name : JOHN is repeated 4 times, all Cust_ID are different when user select JOHN on first Item i am getting correct "SelectedValue" 客户名称:JOHN重复了4次,当用户在第一个项目上选择JOHN时,所有的Cust_ID都不同,我得到正确的“ SelectedValue”

but if user select 2 nd or 3rd JOHN Combobox Item it allways default select First Item (Name as JOHN) and the SelectedValue allways return the First Item Value. 但是如果用户选择第二或第三JOHN Combobox Item,则始终默认选择First Item(名称为JOHN),并且SelectedValue始终返回First Item Value。

i am not able to find where i am doing wrong, please suggest. 我找不到我做错了的地方,请提出建议。

Keep in mind "SelectedValueChanged" event will fire when combobox being populated. 请记住,填充组合框时将触发“ SelectedValueChanged”事件。 Make sure to un-subscribe to this event before populating the combobox. 在填充组合框之前,请确保取消订阅此事件。 and subscribe again after populating the data. 并在填充数据后再次订阅。

            //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);

It seems that you are very new to it. 看来您很陌生。 And as by my guess, because of your incomplete description, you are trying return the "Selected Value" based on the selected text of the combobox. 就像我的猜测一样,由于描述不完整,您正在尝试根据组合框的选定文本返回“选定值”。 But rather than that you must try to attach a value a to selected text and return that value. 但是,除此以外,您必须尝试将值a附加到所选文本并返回该值。 It will surely solve your problem. 它一定会解决您的问题。

Hope it helps. 希望能帮助到你。

Try changing the following property: 尝试更改以下属性:

cmbCustomar.DropDownStyle = DropDownList;

If your ComboBox has DropDownStyle = DropDown , then the "text" part of the ComboBox is trying to match the first item it can find in the list, and in this case, it ignores the current selected item and finds the first "John" on your list. 如果您的ComboBox具有DropDownStyle = DropDown ,则ComboBox的“文本”部分将尝试匹配它在列表中可以找到的第一个项目,在这种情况下,它将忽略当前选定的项目,并找到第一个“ John”您的清单。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM