1

我不确定回发如何在 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();
}
4

1 回答 1

2

Winforms不包含回帖。您将需要绑定到SelectedIndexChanged(or Itemor Value) 事件来过滤您的第二个下拉列表。

例子:

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

        //Call FillSecondChildren on selection change
        firstChild_drop.SelectedIndexChanged += (o, e) => FillSecondChildren();
    }
于 2013-07-30T14:25:16.417 回答