根据组合框选择更改文本框值

时间:2013-05-02 06:08:51

标签: c# winforms combobox

我是C#的新手,我试图用访问数据库绑定组合框。 我使用列名绑定了组合框,但是根据组合框的选择,我无法在文本框中显示详细信息的值(列)。

在我的数据库中有一个包含3个coloumn的表 1.id 2.wesitename 3.Details 和 这是我的代码

 private void button1_Click_1(object sender, EventArgs e)
        {
 string ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\\Users\\nazarmak\\Documents\\newwebsite.accdb;Persist Security Info=True";

        OleDbConnection con = new OleDbConnection(ConnectionString);
        OleDbCommand cmd = new OleDbCommand("select websitename, Details from newweb", con);
        OleDbDataAdapter da = new OleDbDataAdapter();

         DataTable dt = new DataTable();

        try
        {
            con.Open();
            da.SelectCommand = cmd;
            da.Fill(dt);


            this.comboBox1.DisplayMember = "websitename";
            this.comboBox1.ValueMember = "websitename";
            this.comboBox1.DataSource = dt;



        }
        catch (System.Exception ex)
        {
            MessageBox.Show(ex.Message);


        }
        finally
        {

            con.Close();
        }

    }

3 个答案:

答案 0 :(得分:0)

设置

this.comboBox1.ValueMember = "Details";

然后您可以将details作为this.comboBox1.SelectedValue

答案 1 :(得分:0)

或许你可以做这样的事情

private void MyCombobox_SelectedIndexChanged(object sender, EventArgs e)

{
MyTextbox.text=dt.Rows[MyCombobox.SelectedValue]["details"].ToString();
}

我想这就是你要找的东西

答案 2 :(得分:0)

正如我所看到的,您可能将表存储为ComboBox的项目。在这种情况下,如果您想查看每列的列和内容(行详细信息),可以在事件处理程序中尝试:

    DataColumnCollection columns = dt.Columns;
    DataRowCollection rows = dt.Rows;

    foreach (DataColumn column in columns)
    {
        textBox1.AppendText(column.ColumnName + ": ");

        foreach (DataRow row in rows)
        {
            //display the cell value
            textBox1.AppendText(row[column.ColumnName].ToString());
        }

        textBox1.AppendText(Environment.NewLine);

    }
相关问题