如何在连接的ComboBox中找到所选项目的索引(在DATABASE表中)

时间:2012-10-17 00:49:02

标签: c# winforms sql-server-2008 combobox sqldataadapter

在组合框中查找所选项 string 非常简单:

String selectedString = comboBox1.SelectedItem.ToString();

在组合框中查找所选项目 index 也很简单:

int selectedIndex = comboBox1.SelectedIndex; 

但是在组合框中找到所选项目的连接数据库表中的索引似乎并不那么简单:

    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);
    }

我可以尝试以某种方式从所选项目字符串中获取DB表的索引,但这并不能保证唯一性。

在组合框中找到所选项目的连接数据库表中的索引的正确方法是什么?

1 个答案:

答案 0 :(得分:2)

您的组合框选择索引是DataTable中的索引。