根据用户选择填充列表框

时间:2012-09-23 07:43:23

标签: c# xml

  1. 我使用C#来做这件事。
  2. 好的,这就是我的XML文件的样子......只有更多的AUCCars节点。

    <Recordset>
    <AUCCars>
        <Web_Series>5 Series</Web_Series>
            <Body_Desc>Sedan</Body_Desc>
            <Model_Type>550i (E60)</Model_Type>
            <Model_Year>2006</Model_Year>
            <Ext_Colour>Alpine White III - Non-Metallic</Ext_Colour>
            <Int_Colour>Leather Dakota Black - Dakota Leather</Int_Colour>
            <Price>R 579000</Price>
            <Search_Price>579000</Search_Price>
            <Province>Gauteng</Province>
            <AucID>106288</AucID>
            <DealerID>45</DealerID>
            <DealerCode>29968</DealerCode>
            <DealerName>Lyndhurst Auto</DealerName>
            <Transmission>Automatic</Transmission>
            <AirCon>n/a</AirCon>
            <Radio>Yes</Radio>
            <PSteering>Yes</PSteering>
            <ABS>Yes</ABS>
            <Airbag>Yes</Airbag>
            <Sunroof>Yes</Sunroof>
            <Km>11500</Km>
            <Motorplan>Yes</Motorplan>
            <Warranty>N</Warranty>
            <BodyNo>6CR76051</BodyNo>
            <ModelOEM>NB52</ModelOEM>
            <ColourOEM>300</ColourOEM>
            <TrimOEM>LCSW</TrimOEM>
            <WheelsOEM></WheelsOEM>
            <Sold>N</Sold>
            <Notes> </Notes>
            <Moreoptions>Automatic Transmission with Steptronic
                         Interior trim finishers, Fine-wood, Poplar Grain Brown, high-gloss
                         Park Distance Control (PDC),front and rear</Moreoptions>
            <Picture>\Vehicle_Pictures\E60\LI\frontview_big_P0300.jpg</Picture>
            <PictureRear>\Vehicle_Pictures\E60\LI\rearview_big_P0300.jpg</PictureRear>
            <Interior>\Vehicle_Pictures\E60\LI\Interior\big_Fo_LCSW.jpg</Interior>
    </AUCCars>
    <AUCCars>
            <Web_Series>5 Series</Web_Series>
            <Body_Desc>Sedan</Body_Desc>
            <Model_Type>550i (E60)</Model_Type>
            <Model_Year>2006</Model_Year>
            <Ext_Colour>Black Sapphire - Metallic</Ext_Colour>
            <Int_Colour>Amethyst Black Exclusive Leather - Exclusive Leather</Int_Colour>
            <Price>R 529990</Price>
            <Search_Price>529990</Search_Price>
            <Province>KwaZulu Natal</Province>
            <AucID>111922</AucID>
            <DealerID>17</DealerID>
            <DealerCode>2485</DealerCode>
            <DealerName>Supertech</DealerName>
            <Transmission>Automatic</Transmission>
            <AirCon> Yes</AirCon>
            <Radio>Yes</Radio>
            <PSteering>Yes</PSteering>
            <ABS>Yes</ABS>
            <Airbag>Yes</Airbag>
            <Sunroof>n/a</Sunroof>
            <Km>7000</Km>
            <Motorplan>n/a</Motorplan>
            <Warranty>N</Warranty>
            <BodyNo>6CR75567</BodyNo>
            <ModelOEM>NB52</ModelOEM>
            <ColourOEM>475</ColourOEM>
            <TrimOEM>LDRH</TrimOEM>
            <WheelsOEM></WheelsOEM>
            <Sold>N</Sold>
            <Notes> </Notes>
            <Moreoptions>Automatic Transmission with Steptronic
                         Electric Rear Screen Roller Sun Blind with manual Side Blinds
                         Interior trim finishers, Fine-wood, Poplar Grain Brown, high-gloss
                         High Beam Assist
                         Head-up display (not with SA354)</Moreoptions>
            <Picture>\Vehicle_Pictures\E60\LI\frontview_big_P0475.jpg</Picture>
            <PictureRear>\Vehicle_Pictures\E60\LI\rearview_big_P0475.jpg</PictureRear>
            <Interior>\Vehicle_Pictures\E60\LI\Interior\big_Fo_LDRH.jpg</Interior>  
    </AUCCars>
    <Recordset>
    
  3. 我需要填写系列,型号和年份的组合框。

  4. 我说得对。当我编译程序时,它将所有系列加载到系列
  5. 组合框。
  6. 当我选择一个系列时,它会自动将所有模型加载到模型组合
  7. 盒。当我选择模型时,它会相应地自动加载年份。

  8. 现在我需要根据选择显示数据。

  9. 例如。我为系列组合框选择了“5系列”,为模型组合框11选择了“550i(E60)”,为年份组合框选择了“2006”。

  10. 如何根据我的选择13在列表框中显示该特定节点中的所有数据?

1 个答案:

答案 0 :(得分:1)

我希望我确实帮到你了...... 我创建了一个小样本表单。在 comboBox SelectedIndexChanged 上,我将显示 过滤的aucCars 。 ComboBoxes根据问题(Web_Series, Model_Type, Model_Year - distinct)

中给出的详细信息从xml文件中获取数据
// get data from file
XElement aucCars = XElement.Load("data.xml");
private void cmbSeries_SelectedIndexChanged(object sender, EventArgs e)
{
    if (cmbSeries.SelectedItem != null)
    {
        string currentSeries = cmbSeries.SelectedItem.ToString();
        var models = (from a in aucCars.Elements()
                        where a.Element("Web_Series").Value == currentSeries
                        select a.Element("Model_Type").Value).Distinct().ToList();
        cmbModel.DataSource = models;
    }
    showAucCars();
}
private void cmbModel_SelectedIndexChanged(object sender, EventArgs e)
{
    if (cmbModel.SelectedItem != null)
    {
        string currentSeries = cmbSeries.SelectedItem.ToString();
        string currentModel = cmbModel.SelectedItem.ToString();

        var years = (from a in aucCars.Elements()
                        where a.Element("Web_Series").Value == currentSeries &&
                        a.Element("Model_Type").Value == currentModel
                        select a.Element("Model_Year").Value).Distinct().ToList();
        cmbYear.DataSource = years;
    }
    showAucCars();
}
private void cmbYear_SelectedIndexChanged(object sender, EventArgs e)
{
    showAucCars();
}
private void frmXmlLoad_Load(object sender, EventArgs e)
{            
    var series = (from a in aucCars.Elements()
                select a.Element("Web_Series").Value).Distinct().ToList();
    cmbSeries.DataSource = series;
}
private void showAucCars()
{
    var filterCars = aucCars.Elements();
    if (cmbSeries.SelectedItem!=null)
    {
        string currentSeries = cmbSeries.SelectedItem.ToString();
        filterCars = from a in filterCars
                        where a.Element("Web_Series").Value == currentSeries
                        select a;
    }
    if (cmbSeries.SelectedItem != null)
    {
        string currentModel = cmbModel.SelectedItem.ToString();
        filterCars = from a in filterCars
                        where a.Element("Model_Type").Value == currentModel
                        select a;
    }
    if (cmbSeries.SelectedItem != null)
    {
        string currentYear = cmbYear.SelectedItem.ToString();
        filterCars = from a in filterCars
                        where a.Element("Model_Year").Value == currentYear
                        select a;
    }
    // will show all the element data 
    // add a new linq stmt to select specific elements
    listBox1.DataSource = filterCars.ToList();            
}

showAucCars()根据comboBoxes 中的选择进行过滤,并在listBox中显示结果。如果您想在列表框中显示特定元素,请添加新的linq stmt并选择相应的元素