[英]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
那么您可以获取
details
为this.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.