简体   繁体   English

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

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

I am new in C# here i am trying to bind combo box with access database. 我在C#中是新手,我在这里尝试将组合框与Access数据库绑定。 I bound the combo box with column name but i can not display the value of details(Column) in text box based on the selection of combo box. 我用列名绑定了组合框,但是根据组合框的选择,我无法在文本框中显示details(Column)的值。

in my database there is a table which contain the 3 coloumn 1.id 2.wesitename 3.Details and this is my code 在我的数据库中有一个表,其中包含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();
        }

    }

set

this.comboBox1.ValueMember = "Details";

then you can get details as this.comboBox1.SelectedValue 那么您可以获取detailsthis.comboBox1.SelectedValue

Or maybe you can do something like this 或者也许你可以做这样的事情

private void MyCombobox_SelectedIndexChanged(object sender, EventArgs e)

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

I guess this is what you are looking for 我想这就是你要找的

As I see, you might be storing tables as the items of ComboBox. 如我所见,您可能将表存储为ComboBox的项。 In this case if you want to look into columns and contents of each column (row details) you can try this in your event handler: 在这种情况下,如果您要查看列和每列的内容(行详细信息),可以在事件处理程序中尝试以下操作:

    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.

相关问题 C#Windows窗体如何根据第一个组合框中的选择更改第二个组合框的值 - C# Windows Forms how to change values of second combo box based on selection in first combo box 如何根据从其他框选择的文本更改动态创建的组合框的显示项目 - how to change the display items of a dynamically created combo box according to the text selected from other box 想要使用组合框选择更改标签 - Wanting to change a label using a combo box selection 使用UI自动化更改WinForms组合框选择 - Change WinForms combo box selection with UI Automation 使用“组合框选定的更改”更改浮点值 - Changing Float Values with “Combo Box Selected Change” 在XAML中更改组合框的默认文本样式 - Change style of default text of Combo Box in XAML WPF - 在组合框中更改枚举的字符串值 - WPF - Change string values of enum in combo box WPF模板组合框列中的选择更改会更改其他行中的值 - selection change in wpf template combo box column changes values in other rows C#使用仅1个选择的txt文件中的值填充2个组合框和1个文本框 - C# populating 2 combo boxes and 1 text box with values from a txt file with only 1 selection WPF应用程序中第一个组合框选择更改时将数据填充到第二个组合框 - populating data to second combo box on first combo box selection change in wpf application
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM