简体   繁体   中英

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. 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 "

I have populated the clustername combo box but I am unable to populate the corresponding 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

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

<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. You just need to fix the 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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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