Note that there are some explanatory texts on larger screens.

plurals
  1. POc# Binding values to combobox when editing
    text
    copied!<p>I have a windows forms application that has several comboboxes populated with sql database table values.</p> <p>this is a example of the code for one:</p> <pre><code>public void brandSelectCB(ComboBox cb) { string sSQL = " SELECT" + " id, name" + " FROM" + " tbBrand" + " ORDER BY" + " name"; sqlConnect connect = new sqlConnect(); DataTable dt = new DataTable(); dt = connect.getBD(sSQL); cb.DataSource = dt; cb.DisplayMember = "name"; cb.ValueMember = "id"; </code></pre> <p>My main form has several records like: model, brand, type and when i want to edit a specific recor, I select the record and click the edit button, that enters the values into the corresponding textboxes and comboxes, like this:</p> <pre><code>private void btnEdit_Click(object sender, EventArgs e) { this.txtID.Text = lvMain.SelectedItems[0].SubItems[0].Text; this.cbBrand.SelectedText = lvMain.SelectedItems[0].SubItems[1].Text; this.cbModel.SelectedText = lvMain.SelectedItems[0].SubItems[2].Text; this.txtName.Text = lvMain.SelectedItems[0].SubItems[3].Text; this.cbType.SelectedText = lvMain.SelectedItems[0].SubItems[4].Text; } </code></pre> <p>works fine, i get the text to the combobox and texboxes, but the values of the comboboxes aren't selected, only the text is. if I chose <code>SelectedValue</code> or <code>SelectedItem</code> I get nothing. I have the corresponding text from the selected item but I have to choose all over again the values from the comboboxes :( </p> <p>was i clear ? :P</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload