如何在ComboBox SelectedIndexChanged事件的文本框中获取DataTable列的值?

时间:2019-01-20 06:18:13

标签: datatable combobox selectedindexchanged

我有一个组合框'cbxProject',其中使用以下代码填充了Projects数据:

 using (OleDbConnection connection = new OleDbConnection(PrjConn))
        {
            connection.Open();
            OleDbCommand comm1 = new OleDbCommand("SELECT prj1.PROJ_REF, PTITLE FROM prj1 ORDER BY prj1.CONTRACTNO", connection);
            adapt.SelectCommand = comm1;
            adapt.Fill(tblProjects);
            //Bind Comboboxes
            //PROJ_REF Combobox
            cbxProject.ValueMember = "PROJ_REF";
            cbxProject.DisplayMember = "PROJ_REF";

            cbxProject.DataSource = tblProjects;
        }

在C#表单上,每当组合框'cbxProject'SelectedIndexChanged如下时,我都有一个文本框'txtTitle'以获取项目标题'PTITLE':

 private void cbxProject_SelectedIndexChanged(object sender, EventArgs e)
    {
        txtTitle.Text = tblProjects.Rows[0]["PTITLE"].ToString();
    }

但是我在txtTitle.Text中仅获得Rows [0]值。每次更改SelectedIndex。

1 个答案:

答案 0 :(得分:0)

我已经通过以下修改对这个问题进行了排序:

private void cbxProject_SelectedIndexChanged(object sender, EventArgs e)
    {

        int row = cbxProject.SelectedIndex;
        txtTitle.Text = comboDataSet.Tables["tblProjects"].Rows[row]["PTITLE"].ToString();


    }