Finding the selected item string in a combo box is easy:
String selectedString = comboBox1.SelectedItem.ToString();
Finding the selected item index in a combo box is also easy:
int selectedIndex = comboBox1.SelectedIndex;
But finding the index in a connected database table of the selected item in the combo box doesn’t seem so trivial:
DataTable dt = new DataTable();
SqlDataAdapter da = new SqlDataAdapter("SELECT * FROM tblContacts ORDER BY colFirstname", sqlConnection);
da.Fill(dt);
for (int i = 0; i < dt.Rows.Count; i++)
{
String addressRow = (String)dt.Rows[i]["colFirstname"];
comboBox1.Items.Add(addressRow);
}
I could try to somehow derive an index to the DB table from the selected item string, but that doesn’t guarantee uniqueness.
What is the proper way of finding the index in a connected database table of the selected item in the combo box?
Your combobox selected index is the index in DataTable.