1

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

在我的数据库中有一个包含 3 列 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();
        }

    }
4

3 回答 3

0

this.comboBox1.ValueMember = "Details";

那么你可以details得到this.comboBox1.SelectedValue

于 2013-05-02T06:13:45.533 回答
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);

    }
于 2013-05-02T08:03:27.613 回答
0

或者也许你可以做这样的事情

private void MyCombobox_SelectedIndexChanged(object sender, EventArgs e)

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

我想这就是你要找的

于 2013-05-02T06:20:01.477 回答