简体   繁体   中英

Same two items in combobox but first one always gets selected C#

I've got really weird problem with combobox on my windows form application.

So my combobox is populated using datasource, it displays names of people and it holds their IDs as cmbRequestor.ValueMember.

public BindingSource requestorBindingSource = null;
private const string cmdAssoc = "SELECT * FROM assoc_vw ORDER BY assoc_name";
requestorBindingSource.DataSource = populateDataTable(cmdAssoc);

cmbRequestor.DisplayMember = "assoc_name";
        cmbRequestor.ValueMember = "ID";
        cmbRequestor.DataSource = requestorBindingSource;
        cmbRequestor.SelectedIndex = 0;

在此输入图像描述

It works fine but if there is an instance of people with the same name and I select 2nd name (of the same name) from the combobox, for some reason once I close the combobox it selects the first name even though I selected 2nd name.

在此输入图像描述

So to make sure they hold different values against their names I have created SelectedIndexChanged event.

private void cmbRequestor_SelectedIndexChanged(object sender, EventArgs e)
    {
        int x = cmbRequestor.SelectedIndex;
        string j = cmbRequestor.SelectedValue.ToString();
        var y = cmbRequestor.Items[x];
    }

When I debug the code and I select 2nd name (of the same name) the ID behind it is 3069. Once I close the combobox and click save to save the form SelectedIndexChanged is triggered again (that should not happen) and it goes to the first person with the same name and its ID is different.

There are no other events on this control and I dont use it anywhere else. It looks like the control gets confused itself if there is an instance of the same name.

Change DropDownStyle property to DropDownList.
Default value is DropDown and in that case selected item will be determined by the first matched text in the list. DropDown is mainly used in conjunction with autocomplete logics.

EDIT:
If you have to stick with DropDown style, the best workaround will be to handle DropDownClosed event, at that point you will have the correct index selected.

我发现如果我在Properties中将FormattingEnabled设置为false,那就可以了。

I had also the same problem... The best solution for me was to change the DropDown Style property of the combo Box to DropDownList. When I needed the DropDown style (eg to input new data in the Combo Box) I was changing the property to DropDown in the code... and changing back to DropDownList when finished.

尝试使用组合框的ComboBox.SelectionChangeCommitted事件,也许您需要删除默认选择的索引,该索引设置为零

conn1 = JdbcConn.getConn();

            try
            {

                conn1.Open();

                String sqllogin = "Select *from tbladdpattern  ";
                var cmd = new MySqlCommand(sqllogin, conn1);//This is sql query execute
                var reader = cmd.ExecuteReader();//Execute query

                IList<string> listName = new List<string>();
                while (reader.Read())
                {
                    listName.Add(reader[1].ToString());
                }
               // listName = listName.Distinct().ToList();
                comboBox1.DataSource = listName.Distinct().ToList();



                conn1.Close();//Close DataBase Connection
            }
            catch (Exception ex)
            {
                conn1.Close();
                LogCreate.WriteLog("Errorn in show all pattern " + ex);
            }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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