繁体   English   中英

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

[英]change the text box values according to the combo box selection

我在C#中是新手,我在这里尝试将组合框与Access数据库绑定。 我用列名绑定了组合框,但是根据组合框的选择,我无法在文本框中显示details(Column)的值。

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

    }

this.comboBox1.ValueMember = "Details";

那么您可以获取detailsthis.comboBox1.SelectedValue

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

private void MyCombobox_SelectedIndexChanged(object sender, EventArgs e)

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

我想这就是你要找的

如我所见,您可能将表存储为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);

    }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM