【问题标题】:How to Match two comboboxes items together如何将两个组合框项目匹配在一起
【发布时间】: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的事件

标签: c# winforms combobox


【解决方案1】:

如果我对您的理解正确,并且如果您在 SQL 服务器或其他数据库中有此数据,您应该使用 SelectedIndexChange 事件并为该 ID 加载项目(使用显示成员和值成员作为匹配 ID)。

看看this。它可能对你有帮助

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

        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);
            }
        }  

编辑 2:

在 cb1.SelectedValueChange 中使用此代码:

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;

您还可以创建一个过程,将表名中的项目作为输入发回。如果你使用过程,你的代码性能会更好。

编辑 3:

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

        try
        {
            int a = int.Parse(cbTB2.SelectedValue.ToString());
        }
        catch { }

【讨论】:

  • Thnx 但我使用 sql server 来填充 cb1。帮忙?
  • 我改了代码,但是因为不知道你的数据库字段,所以无法完成查询。
  • 非常感谢,我要试试运气:)
  • 此代码应该可以正常工作,您应该尝试根据您的需要更改示例代码。答案不可能完全是你想要的。
  • 嗨,Pedram,代码运行不正常,所以我稍微编辑了一下。好消息是它用 System.Data.Data RowView 填充 cb2 但我不知道。你能看看吗?
【解决方案2】:

您可能希望查看组合框的事件,“SelectedIndexChanged”或“SelectedValueChanged”应该这样做

【讨论】:

  • 您好,我是 c# winforms 的新手。在哪里 shd 我应用 selectedindex 已更改,以便填充 cb2。如果你给我一个例子会是新的
  • Thnx 但我还是不明白。我正在使用 sql server 填充 cb1 因此,如果选择了一个表,则定义的列名应该填充 cb2。这可能吗?
  • 是的,对于选择的每个表名,获取combobox1项文本(表名)并尝试再次执行sql请求以获取所有列名并将其添加到combobox2中--最简单的逻辑
猜你喜欢
  • 1970-01-01
  • 2022-08-03
  • 2021-08-05
  • 2011-07-22
  • 2023-03-22
  • 2013-11-27
  • 1970-01-01
  • 2023-03-24
  • 2015-08-04
相关资源
最近更新 更多