根据用户选择动态更新组合框

时间:2013-07-30 14:20:25

标签: c# winforms combobox

我不确定回发如何在WinForms上运行,但我想允许ComboBox根据用户选择进行更新。

目前,当我更改第一个ComboBox的选择时,它不会更改第二个下拉列表中的项目。 (默认只显示第一项)

O可以通过哪些方式改变这一点?

我所拥有的代码:

public ContentUploader()
        {
            InitializeComponent();

            using (SqlConnection con = new SqlConnection(conString))
            {
                con.Open();
                using (SqlDataAdapter sda = new SqlDataAdapter("SELECT ... re.OverallID = 1", conString))
                {
                    DataTable dt = new DataTable();
                    sda.Fill(dt);

                    sections_drp.ValueMember = "ID";
                    sections_drp.DisplayMember = "DisplayName";
                    sections_drp.DataSource = dt;
                }
            }
            sections_drp.SelectedIndexChanged += (o, e) => FillFirstChildren();
        }
        public void FillFirstChildren()
        {
            firstChild_drp.Items.Add("Select Item");
            firstChild_drp.SelectedIndex = 0;

            string sectionId = sections_drp.SelectedValue.ToString();

            using (SqlConnection con = new SqlConnection(conString))
            {
                con.Open();
                using (SqlDataAdapter sda = new SqlDataAdapter("SELECT ... em.ItemID = ("+ sectionId +")", conString))
                {
                    DataTable dt = new DataTable();
                    sda.Fill(dt);

                    firstChild_drp.ValueMember = "ID";
                    firstChild_drp.DisplayMember = "DisplayName";
                    firstChild_drp.DataSource = dt;
                }
            }
            FillSecondChildren();
        }

1 个答案:

答案 0 :(得分:2)

Winforms不包含回帖。您需要与SelectedIndexChanged(或ItemValue)事件绑定,以过滤您的第二个下拉列表。

示例:

    public void FillFirstChildren()
    {
        //Your Fill Logic Here
        ...

        //Call FillSecondChildren on selection change
        firstChild_drop.SelectedIndexChanged += (o, e) => FillSecondChildren();
    }