Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/318.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何将两个组合框项目匹配在一起_C#_Winforms_Combobox - Fatal编程技术网

C# 如何将两个组合框项目匹配在一起

C# 如何将两个组合框项目匹配在一起,c#,winforms,combobox,C#,Winforms,Combobox,我有两个组合框,其中应该包含两个不同的信息 1.cb1:从信息\u架构中选择表\u名称。此表显示多个表 2.cb2:应使用列名填充它 示例:cb1中有三个表具有相同的属性,但列EmpName tblLondon、tblBerlin、tblRom等处的值不同 现在,每当我在第一个组合框中选择一个表时,我想在第二个组合框中动态显示列EmpName cb1[tblLondon] cb2[John,Mavis,Chris,Mike..] 或 你能帮我吗

我有两个组合框,其中应该包含两个不同的信息

1.cb1:从信息\u架构中选择表\u名称。此表显示多个表 2.cb2:应使用列名填充它

示例:cb1中有三个表具有相同的属性,但列EmpName tblLondon、tblBerlin、tblRom等处的值不同

现在,每当我在第一个组合框中选择一个表时,我想在第二个组合框中动态显示列EmpName

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

你能帮我吗

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)
        {


        }

如果我理解正确,并且如果您在SQL server或其他数据库中有此数据,则应使用SelectedIndexChange事件并为该Id加载项,并为匹配Id使用显示成员和值成员

看一看,也许对你有帮助

编辑: 您可以使用以下代码执行此操作:如果不使用数据库 您应该在cb1.SelectedIndexChange或value中使用此代码

        var cb2items = new Dictionary<int, string> {{1, "Name"}, {1, "anotherName"},{2,"Name"},{2, "anotherName"}}; // use the number for parent Id in cb1
        foreach (var item in cb2items)
        {
            if (item.Key == int.Parse(comboBox1.SelectedValue.ToString()))
            {
                comboBox2.Items.Add(item);
            }
        }  
您还可以创建一个过程,将表名中的项作为输入发送回。如果使用过程,代码的性能会更好

编辑3:

将此代码放入cbTb2.SelectedValueChange事件:


您可能想查看combobox的事件,SelectedIndexChanged或SelectedValueChanged应该可以这样做

您可能想查看该事件。我假设您使用的是information\u schema.tables,因为您在设计时不知道所有的表?我之所以使用它,是因为我希望能够随时动态更改表名,正如我所理解的,您只想基于combobox1填充combobox2?如果是这样的话,就不需要设置cb2的事件,只需要设置cbtbl的事件。我是WinC窗体的新手。其中shd i应用已更改的selectedindex,以便填充cb2。如果你给我举个例子,我会觉得很新鲜,但我还是不明白。我正在使用sql server填充cb1,因此如果选择了表,则定义的列名应该填充cb2。可能吗?是的,对于选择的每个表名,获取combobox1项texttablename,然后再次尝试执行sql请求以获取所有列名并将其添加到combobox2-最简单的logicThnx中,但我使用sql server填充cb1。帮助?我更改了代码,但因为我不知道您的数据库字段,所以无法完成查询。Thanx我非常想试试我的运气:这些代码应该正常工作,您应该尝试更改示例代码以满足您的需要。答案不可能完全是你想要的。嗨,佩德拉姆,代码工作不正常,所以我稍微编辑了一下。好的是,它填充了cb2,但是使用System.Data.Data RowView,我不知道。你能看看吗?
        var cb2items = new Dictionary<int, string> {{1, "Name"}, {1, "anotherName"},{2,"Name"},{2, "anotherName"}}; // use the number for parent Id in cb1
        foreach (var item in cb2items)
        {
            if (item.Key == int.Parse(comboBox1.SelectedValue.ToString()))
            {
                comboBox2.Items.Add(item);
            }
        }  
string C = ConfigurationManager.ConnectionStrings[""].ConnectionString;
    SqlConnection con = new SqlConnection(C);
    SqlCommand cmd = new SqlCommand();
    cmd.Connection = con;
    cmd.CommandText = ("SELECT ... WHERE TableName = cb1.SelectedValue");
      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";
        cbTb2.DataSource = dt;
        try
        {
            int a = int.Parse(cbTB2.SelectedValue.ToString());
        }
        catch { }