SelectedIndexChanged starts automatically without selecting items in combobox in a C # windows application

I have a combo box that is populated from the database. Code below:

protected void bindcombobox() { string str = "Data Source=IMMENSE-01\\SQLEXPRESS;Initial Catalog=DesktopAppDB;Integrated Security=True;Pooling=False"; SqlConnection con = new SqlConnection(str); con.Open(); SqlCommand cmd = new SqlCommand("select Id,designation from addStaff", con); SqlDataAdapter da = new SqlDataAdapter(cmd); DataTable dt = new DataTable(); DataSet ds = new DataSet(); da.Fill(ds); da.Fill(dt); cb_selectpost.Items.Clear(); if (dt.Rows.Count > 0) { cb_selectpost.DataSource = dt; cb_selectpost.ValueMember = "Id"; cb_selectpost.DisplayMember = "designation"; } con.Close(); con.Dispose(); } 

I have this event that fires when an item is selected from combobox as

  private void cb_selectpost_SelectedIndexChanged(object sender, EventArgs e) { if (cb_selectpost.SelectedValue != null) { string st = cb_selectpost.SelectedValue.ToString(); lblCB.Text = st.ToString(); bindstaff(st); } } 

But the problem is that this event is automatically fired when the project starts, not allowing me to select an item from the combo box.

0
c # combobox
Nov 16 '14 at 10:37
source share
1 answer

You can subscribe to the SelectionChangeCommitted event instead of the SelectedIndexChanged event. or you can unsubscribe from the SelectedIndexChanged event in front of the palms of the data and subscribe after loading the data in the combo box. You can solve this problem in several ways.

+2
Nov 16 '14 at 10:53
source share



All Articles