如何在C#中设计两个相互依赖的组合框? [英] How to design two combo boxes which are depend on each other in C#?

查看:188
本文介绍了如何在C#中设计两个相互依赖的组合框?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的Windows窗体应用程序中,有两个组合框。第一个是品牌代码,第二个是品牌名称。我在表单加载时将所有数据加载到两个组合框。当我从代码列表中选择一个品牌代码时,相关的品牌名称会显示在它的组合框(第2个)中。没关系。

但是我还想要其他方式,因为当选择品牌名称品牌代码应该显示在它的组合框中。

我使用微软访问数据库。

如何在一个界面上实现这两个流程?



我尝试过:



In my windows form application, there are two combo boxes. 1st one is for brand code and 2nd one is for brand name. I load all data to both combo boxes at form load. When I select one brand code from the code list, relevant brand name is displayed it's combo box (2nd). It is ok.
But I want to other way round also, as when selects brand name brand code should be displayed in it's combo box.
I use Microsoft access Database.
How to implement both processes at one interface?

What I have tried:

//form load event

private void Brands_Load(object sender, EventArgs e)
{
    try
    {
        con.Open();
        OleDbDataReader reader = null;
		OleDbCommand cmd = new OleDbCommand("Select * From Brand", con);
        reader = cmd.ExecuteReader();               
                  
        while (reader.Read())
        {

            cmbbCode.Items.Add(reader["BCODE"].ToString());
            cmbbName.Items.Add(reader["BNAME"].ToString());
        }
                
        con.Close();

    }
    catch (Exception ex)
    {
        MessageBox.Show("error " + ex);
    }
}

//cmbBcode change value event

		private void cmbbCode_SelectedIndexChanged(object sender, EventArgs e)
        {
            try
            {
                String Bcode = cmbbCode.Text.ToString();
                con.Open();
                OleDbDataReader reader = null;
                OleDbCommand cmd = new OleDbCommand("Select BNAME From Brand Where BCODE = '" + Bcode + "'", con);
                reader = cmd.ExecuteReader();
                while (reader.Read())
                {
                    String Name = reader["BNAME"].ToString();
                    cmbbName.Text = Name;
                }
                con.Close();
            }
            catch (Exception ex)
            {
                MessageBox.Show("error " + ex);
            }
        }
        }

推荐答案

我不这样做。相反,不要使用DateReader,使用DataAdapter,并使用.NET内置代码来访问数据。

此代码使用SQL Server而不是OleDb,但对于您的数据库也是如此 - 我碰巧打开了SSMS,所以我现在更容易使用SQL Server:

I wouldn't do it like that. Instead don't use a DateReader, use a DataAdapter, and use the .NET built in code to access the data.
This code uses SQL Server instead of OleDb, but it's the same for your DB - I happen to have SSMS open so it's easier for me to play with SQL Server at the moment:
using (SqlConnection con = new SqlConnection(strConnect))
    {
    DataTable dt = new DataTable();
    using (SqlDataAdapter da = new SqlDataAdapter("Select BCODE, BNAME FROM Brand", con))
        {
        da.Fill(dt);
        cmbbCode.DataSource = dt;
        cmbbCode.DisplayMember = "BCODE";
        cmbbName.DataSource = dt;
        cmbbName.DisplayMember = "BNAME";
        cmbbCode.SelectedIndex = 0;
        }
    }

这将从您的数据库中读取数据,并告诉每个ComboBox显示来自同一记录的不同值。

然后将它们设置为通过相同的处理程序处理SelectedIndexChanged事件:

This reads the data from your DB, and tells each ComboBox to display a different value from the same record.
Then set them both to handle the SelectedIndexChanged event via the same handler:

private void cmbBoth_SelectedIndexChanged(object sender, EventArgs e)
    {
    ComboBox cb = sender as ComboBox;
    if (cb != null && cmbbCode.DataSource != null && cmbbName.DataSource != null)
        {
        int index = cb.SelectedIndex;
        if (index >= 0)
            {
            if (cmbbCode.SelectedIndex != index) cmbbCode.SelectedIndex = index;
            if (cmbbName.SelectedIndex != index) cmbbName.SelectedIndex = index;
            }
        }
    }

处理程序从用户刚刚更改的ComboBox中获取新索引,并将其设置为两者。空检查确保两者都已加载(或者你得到索引超出范围的错误)和它是否是相同的索引?检查确保我们没有得到无限循环的索引更改!

尝试一下:它比你想象的要简单得多,它让系统为你处理一切,这是比你的解决方案更清洁。

The handler gets the new index from whichever ComboBox the user just changed, and sets it to them both. The null checks make sure that both are loaded already (or you'd get an error that the index is out of range) and the "is it the same index?" checks make sure that we don;t get an infinite loop of index changes!
Try it: it's a lot simpler to use than you might think, and it lets the system handle everything for you, which is much cleaner than your solution.


这篇关于如何在C#中设计两个相互依赖的组合框?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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