user1800674
user1800674

Reputation: 233

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;

enter image description here

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.

enter image description here

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.

Upvotes: 4

Views: 5476

Answers (5)

LearnTo LearnTech
LearnTo LearnTech

Reputation: 11

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

Upvotes: 0

Vasilis
Vasilis

Reputation: 11

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 (e.g. 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.

Upvotes: 1

OpticFusion
OpticFusion

Reputation: 11

I found that if I set FormattingEnabled to false in Properties, that it works.

Upvotes: 1

E-Bat
E-Bat

Reputation: 4892

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.

Upvotes: 5

MasterBettor
MasterBettor

Reputation: 109

try using ComboBox.SelectionChangeCommitted Event for the combobox and maybe you need to remove the default selected index which is set to zero

Upvotes: 0

Related Questions