Here is an easy way. You can use the Tag property for combobox. It can be empty or 0 is an integer value when it is empty or not yet executed. You must set the combobox tag as the number of elements after the limit. In the SelectedValueChanged event, if the Tag property is null or 0, you must return from void.
Here are some examples from my project.
private void cb_SelectedValueChanged(object sender, EventArgs e) { if (!(sender is ComboBox)) return; ComboBox cb = sender as ComboBox; if (DataUtils.ToInt(cb.Tag, 0) == 0) return; if (cbSmk.SelectedValue == null ) return; } public static void ComboboxFill(ComboBox cb, string keyfld, string displayfld, string sql) { try { cb.Tag = 0; cb.DataSource = null; cb.Items.Clear(); DataSet ds = DataMgr.GetDsBySql(sql); if (!DataUtils.HasDtWithRecNoErr(ds)) { cb.Text = "No data"; } else { cb.DataSource = ds.Tables[0]; cb.DisplayMember = displayfld; cb.ValueMember = keyfld; } cb.Tag = cb.Items.Count; } catch (Exception ex) { Int32 len = ex.Message.Length > 200 ? 200 : ex.Message.Length; cb.Text = ex.Message.Substring(0, len); } } CmpHelper.ComboboxFill(cbUser, "USER_ID", "USER_NAME", "SELECT * FROM SP_USER WHERE 1=1 ORDER by 1",-1);
Rasulbek
source share