如何链接两个组合框 [英] How to link two comboboxes

查看:82
本文介绍了如何链接两个组合框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

I’ve two comboboxes which should contain two different informations.

1.cb1: select table_name from information_schema.tables (this display multiple tables)
2.cb2: should populate it with a column name.

Example: I've three tables in cb1 with the same attributes but have different values at the column EmpName (tblLondon,tblBerlin,tblRom,...)

Now I wanna display in second comboboxe the column EmpName dynamically whenever I choose a table in first combobox.

cb1                  cb2
[tblLondon]          [John,Mavis,Chris,Mike..] 
            
OR

cb1                    cb2
[tblBerlin]       [Günther,Peter, Sophie,Sunny, ..]


Can u plz help me out



这个代码正在工作并填充我的cb1(组合框)。








This code is working and populating my cb1 (combobox).



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;







下面的代码填充组合框1但是如果是System.Data.DataView。

怎么了?






The code below is populating the combobox 1 BUT if System.Data.DataView.
What's wrong?

private void comboBox1_SelectedIndexValue(object sender, EventArgs e)
        {
if (cbTbl.SelectedValue != null) 
{ 
string C = ConfigurationManager.ConnectionStrings[""].ConnectionString; SqlConnection con = new SqlConnection(C); 
SqlCommand sqlCmd = new SqlCommand(); 
sqlCmd.Connection = con; 
sqlCmd.CommandText = (" SELECT [Projektdefinition] FROM " + cbTbl.SelectedValue.ToString() + ""); 
SqlDataAdapter sqlDataAdap = new SqlDataAdapter(sqlCmd);
 try 
{ 
con.Open(); 
dt = new DataTable(); 
sqlDataAdap.Fill(dt); 
comboBox1.DataSource = dt; 
} 
catch (Exception ex) 
{
 MessageBox.Show(ex.Message); 
} 
finally 
{ 
con.Close(); 
}

        }

推荐答案

由于您使用的是SqlCommand,当索引更改时:< br $>


Since you are using SqlCommand, when index changes do:

int i = combo1.getSelectedIndex;
if(i>=0)
{
// get item value from combo1
//create sql query for that item
//fill combo 2 with results
}





另一种更简单的方法是创建一个DataSet和TableAdapter并将数据绑定到组合框。您可以自己做或向项目添加数据集并从数据库中添加表。构建数据集之后,在表单设计器中,工具箱中就可以使用适配器。

从那里将combo1绑定到一个表和字段,第二个使用外键或您创建的查询数据集设计师。然后,combo1中的任何更改都将自动更新combo2。



Another, easier way is to create a DataSet and TableAdapter and bind the data to the comboboxes. You can do yourself or add a dataset to your project and add tables from your database. After building the dataset and adapters will be available to you in toolbox when in form designer.
From there you bind combo1 to one table and field and the second using the foreign key or a query which you create in dataset designer. Then any changes in combo1 will automatically update combo2.


这篇关于如何链接两个组合框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆