I have a comboBox that displays different municipalities (these municipalities belong to a certain province) in our country. Since there are municipalities of the same name, I linked the "MunicipalityName" (the table column from the "MUNICIPALITY" table in my database) to the DisplayMember property of the comboBox property and the "Municipality_ID" for the ValueMember property of comboBox.
When the user saves his data, I provide the SelectedValue from the ValueMember MUNICIPALITY and insert it into the Employee table.
cmd.Parameters.Add(new SqlParameter("@Municipality_ID", (object)comboBoxMunicipality.SelectedValue.ToString()));
It’s difficult for me when it comes to obtaining data, when an Employee needs to update his information. I have to manually check the Municipality_ID of this employee and compare it with the attached data in the comboBox, then skip it, determine which index is in the Municipality_ID, and set the SelectedIndex property for the comboBox. (Silent length compared to the code snippet below)
I have this code, but I find conflicts, since Municipality_Name is not unique.
//set SelectedIndex based from DisplayMember of the comboBox
comboBoxMunicipality.SelectedIndex = comboBoxMunicipality.FindStringExact(dataTable.Rows[0]["MunicipalityName"].ToString());
Is there a way to set SelectedIndex from comboBox, like the code above, but this time by comparing it to ValueMember?
Is there a shortcut?
comboBoxMunicipality.SelectedIndex =
comboBoxMunicipality.FindByValue(dataTable.Rows[0]["Municipality_ID"].ToString());
I hope you understand my guys ... Please help. Thank.
source
share