【发布时间】:2015-01-23 22:04:17
【问题描述】:
我有两个组合框,应该包含两个不同的信息。
1.cb1:从 information_schema.tables 中选择 table_name(显示多个表)
2.cb2:应该用列名填充它。
示例:我在 cb1 中有三个表具有相同的属性,但在列 EmpName (tblLondon,tblBerlin,tblRom,...) 具有不同的值
现在,每当我在第一个组合框中选择表时,我想在第二个组合框中动态显示 EmpName 列。
cb1[tblLondon] cb2[John,Mavis,Chris,Mike..]
或
cb1[tblBerlin] cb2[Günther,Peter, Sophie,Sunny, ..]
你能帮帮我吗
string C = ConfigurationManager.ConnectionStrings[""].ConnectionString;
SqlConnection con = new SqlConnection(C);
SqlCommand cmd = new SqlCommand();
cmd.Connection = con;
cmd.CommandText = ("SELECT TABLE_NAME FROM INFORMATION_SCHEMA.TABLES ORDER BY TABLE_NAME ASC");
try
{
// Open connection, Save the results in the DT and execute the spProc & fill it in the DT
con.Open();
SqlDataAdapter adapter = new SqlDataAdapter(cmd);
dt = new DataTable();
adapter.Fill(dt);
cbTbl.DisplayMember = "TABLE_NAME";
cbTbl.ValueMember = "TABLE_NAME";
//Fill combobox with data in DT
cbTbl.DataSource = dt;
// Empty bzw. clear the combobox
cbTbl.SelectedIndex = -1;
此代码正在运行并填充我的 cb1(组合框)
现在我真的不知道如何处理 cb2
private void comboBox2_SelectedIndexChanged(object sender, EventArgs e)
{
}
【问题讨论】:
-
您可能想查看ComboBox.SelectedIndexChanged 事件。
-
我假设您使用的是 information_schema.tables,因为您在设计时不知道所有表?
-
我使用它是因为我希望能够随时动态更改表名
-
据我了解,您只想根据 combobox1 填充 combobox2 吗?如果是这样,不需要设置cb2的事件只cbtbl的事件