我是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();
}
}
什么是textBox?看起来你想要实现'SelectedIndexChanged'事件,在这个事件中,你将得到comboBox的选定项的索引,并且可以使用它。一个例子是确定“细节”并将一些“textBox.Text”设置为该值。 – Corak 2013-05-02 06:21:16