0
votes

I believe this question is kinda new-bie, but I can't solve it in correct way.

Brief description:

  1. I have an inherited from ComboBox class that does some data bindings in constructor:
    var mdl = new Model();

    ValueMember = "id";
    DisplayMember = "unit";
    DataSource = mdl.getUnits();

All good here. The combobox is filled by required data.

  1. Then I have another form with a function editIngridient. The function is following;
    public bool editIngridient(int id)
    {
    currentId = id;

    var row = mdl.getIngridient(id);

    txtIngridient.Text = (string)row["ingridient"];
    cmbUnit.ID = (int)row["unitId"];
    numNotifyQty.Value = (int) row["notifyQty"];

    ShowDialog();

    return true;
    }
  1. Now, when the form popups, textbox and number box filled by needed values, while combobox is filled by first value.

  2. If I will run the combobox data bind function as the first line inside editIngridient function - all works good.

Please point me to my stupidity.

Thanks a lot!

1
Use the code button in the editor to outline the code in your question. You will get much more response that way.Bas Slagter
If I will change function editIngridient in such way: var row = mdl.getIngridient(id); Show(); txtIngridient.Text = (string)row["ingridient"]; cmbUnit.ID = (int)row["unitId"]; numNotifyQty.Value = (int) row["notifyQty"]; It works.Oleg Romanenko
Another update. If I will set private variables in editIngridient function of unitId, ingridient and notifyQty, and in event Form_Load, I will assign controls values by the private vars set in editIngridient - all works good. So, why I can't assign control(combobox) value directly in editIngridient function?Oleg Romanenko

1 Answers

0
votes

YOu didnt say what is your dataSource, but I assume thats DataTable, so you can do it:

DataRowView rowData = comboBox1.SelectedItem as DataRowView;
int id = Convert.ToInt32(rowData["id"]);
string name = rowData["unit"].ToString();