简体   繁体   English

在另一个组合框的selectedindexchanged事件上填充一个组合框

[英]Populate one combobox on the selectedindexchanged event of another combox

I have been trying to populate some values from an xml file in two separate Combobox. 我一直试图在两个单独的组合框中填充来自xml文件的一些值。 The contents of the second combobox will depend on the selection of the first combo box. 第二个组合框的内容将取决于第一个组合框的选择。 The first combobox will contain the " ClusterName " & the second will contain the corresponding " MachineID " 第一个组合框将包含“ ClusterName ”,第二个组合框将包含相应的“ MachineID

I have populated the clustername combo box but I am unable to populate the corresponding machineid. 我已经填充了clustername组合框,但是无法填充相应的machineid。

The C# code I used to populate the the first combobox:

public void PopulateClusterNameDropDown()
{
    XDocument doc = XDocument.Load("the path of the file");
    List<string> clusterNameList = doc.Root
        .Elements("Machines")
        .Elements("Cluster")
        .Elements("ClusterName")
        .Select(x => (string)x)
        .ToList();

    BindingSource bs = new BindingSource();
    bs.DataSource = clusterNameList;
    cbSelectCluster.DataSource = bs;
}

Edits: 编辑:

The code I tried to populate the Machineid Combo box 我尝试填充Machineid Combo框的代码

private void cbSelectCluster_SelectedIndexChanged(object sender, EventArgs e)
{
    XElement root = XElement.Load("file path");
    if (!cbSelectCluster.Text.Trim().Equals("")) {
        cbSelectMachineID.Enabled = true;
        cbSelectMachineID.Items.Clear();

        var selected = from cli in root.Elements("Machines").Elements("Cluster").Elements("MachineID")
        where cli.Element("ClusterName").Value.Equals(cbSelectCluster.Text)
        select cli;

        BindingSource bs = new BindingSource();
        bs.DataSource = selected;
        cbSelectMachineID.DataSource = bs;
    }
}

The Xml is as follows Xml如下

<Config>
    <Machines>
        <Cluster>
            <ClusterName>ABC</ClusterName>
            <MachineID>Machine123</MachineID>
            <MachineID>Machine456</MachineID>
            <MachineID>Machine789</MachineID>

        </Cluster>
        <Cluster>
            <ClusterName>XYZ</ClusterName>
            <MachineID>Machine111</MachineID>
            <MachineID>Machine222</MachineID>
            <MachineID>Machine333</MachineID>
        </Cluster>
    </Machines>
</Config>

You are getting this issue because you are looking for ClusterName element under MachineID element. 之所以会出现此问题,是因为您要在MachineID元素下寻找ClusterName元素。 You just need to fix the LINQ. 您只需要修复LINQ。

Please find the fixed code below. 请在下面找到固定代码。

Also, it might be worth noting that I have removed the line cbSelectMachineID.Items.Clear(), since you cannot clear a combobox if you set the data-source. 另外,可能值得注意的是,我删除了cbSelectMachineID.Items.Clear()行,因为如果设置数据源,则无法清除组合框。

private void cbSelectCluster_SelectedIndexChanged(object sender, EventArgs e)
{
    XElement root = XElement.Load("filename.xml");
    if (!cbSelectCluster.Text.Trim().Equals(""))
    {
        cbSelectMachineID.Enabled = true;

        var machineIds = root
            .Elements("Machines")
            .Elements("Cluster")
            .Where(clusterElement => (string)clusterElement.Element("ClusterName") == cbSelectCluster.Text)
            .Elements("MachineID")
            .Select(x => (string)x)
            .ToList();

        BindingSource bs = new BindingSource();
        bs.DataSource = machineIds;
        cbSelectMachineID.DataSource = bs;
    }
}

Hope this helps. 希望这可以帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM