populate a combobox based on previous combobox selection in XML

后端 未结 2 945
萌比男神i
萌比男神i 2021-01-22 05:37

I am a .net beginner. I am reading a XML file and showing it in two comboboxes ie., cbProduct and cbBrandName

I need to show text in cbBr

2条回答
  •  孤街浪徒
    2021-01-22 06:05

    In LINQ2XML

    XElement doc=XElement.Load(@"..\..\stock.xml");
    
    //put this code in the form_load event
    cbProduct.Items.AddRange(doc.Descendants("items").Select(x=>x.Element("productname").Value).ToArray());//adds all products
    
    //put this code in the SelectedIndexChanged event of cbProduct
    string product2Search=cbProduct.SelectedItem.ToString();//selected value of cbProduct
    cbBrandNamedoc.Items.Clear(); //clears all items in cbBrandNamedoc
    cbBrandNamedoc.Items.AddRange(doc.Descendants("items").Where(x=>x.Element("productname").Value==product2Search).Select(y=>y.Element("brandname").Value).ToArray());
    

提交回复
热议问题