I have two comboBox cb_Brand
and cb_Model
on a winForm.
cb_Model
populates values on brand Select.
the problem is: if we sele
This would work
combobox.ResetText();
@w69rdy suggests an excellent solution.
The reason cb_Model did not change it's value
is because you never changed the value. cb_Model.Items.Clear() does not change the selected index; only the items are removed from the combo box.
Using the code sample provided in your question:
// Clear Current Data
cb_Model.Text = "";
cb_Model.Items.Clear();
cb_Model.SelectedIndex = -1; // would effectively clear the previously selected value.
I found that keeping the scope of the data source near the loading of the combo box worked for me. I had a datatable with class level scope and it did not clear but then I brought it into function level scope and had it clear after the load and this worked.
Instead of adding the items manually like this:
foreach (Model objM in colM.Values)
{
cb_Model.Items.Add(objM);
}
Let .NET take care of it for you and replace it with this:
cb_Model.DataSource = colMValues;
Which will bind the data to the list and refreshes the comboboxes items automatcially when a data source is set.
You will also not need these lines anymore:
// Clear Current Data
cb_Model.Text = "";
cb_Model.Items.Clear();
Have a read of this for more info on binding lists (and other data sources) to ComboBoxes:
How to: Bind a Windows Forms ComboBox or ListBox Control to Data (MSDN)
I've tried your example. For me it worked as it should have. You could try setting the cb_model.SelectedText to "" or SelectedItem to null
I have a similar problem,tried cmb.resettext it clears text but not value.In my load form I have the below code:
Dim cmd As New SqlCommand("SELECT stud_id,name FROM student_details WHERE stud_id NOT IN (SELECT stud_id FROM student_details WHERE hostel_id!=0)", sqlcont.Conn)
Dim dr As SqlDataReader = cmd.ExecuteReader
Dim dat As New DataTable
Dim j As Integer
For j = 0 To dat.Rows.Count - 1
dr.Read()
Next
dat.Load(dr)
cmbstud.DisplayMember = "name"
cmbstud.ValueMember = "stud_id"
cmbstud.DataSource = New BindingSource(dat, Nothing)
dr.Close()
In my btnhostel click event I have the below code:
frmallocateHostel_Load(Nothing, Nothing)
this I put in attempt to reload my dataset and thus my comboboxes.Using cmbstud.resettext simply clears the text not the value.