mardi 4 août 2015

Using a CheckedListBox as a filter for a DataGridView with an XML file as a datasource

I'm using an XML file loaded into a datagridview, but I want to be able to filter the data based on variable name. Here's an example of the XML:

    <Variable>
    <Name>Logic1</Name>
    <Description/>
    <Values>
        <Value>
            <isstring>FALSE</isstring>
            <value>0.0</value>
            <unit/>
            <date>7/17/2015</date>
            <time>11:16:00 AM,000</time>
            <status>INVALID</status>
        </Value>
        <Value>
            <isstring>FALSE</isstring>
            <value>0.0</value>
            <unit/>
            <date>7/17/2015</date>
            <time>11:17:00 AM,000</time>
            <status>INVALID</status>
        </Value>
        <Value>
            <isstring>FALSE</isstring>
            <value>468.6</value>
            <unit/>
            <date>7/17/2015</date>
            <time>11:18:00 AM,000</time>
            <status>SPONT</status>
        </Value>
        <Value>
            <isstring>FALSE</isstring>
            <value>434.3</value>
            <unit/>
            <date>7/17/2015</date>
            <time>11:19:00 AM,000</time>
            <status>SPONT</status>
        </Value>
        <Value>
            <isstring>FALSE</isstring>
            <value>557.1</value>
            <unit/>
            <date>7/17/2015</date>
            <time>11:20:00 AM,000</time>
            <status>SPONT</status>
        </Value>
    </Values>
</Variable>

And here is my code to populate the datagridview and checkedlistbox with the loaded XML file.

    private void btnLoadXML_Click(object sender, EventArgs e)
    {

        Stream input = null;
        OpenFileDialog loadXML = new OpenFileDialog();
        loadXML.Filter = "XML Files(.*xml)|*.xml";
        loadXML.FilterIndex = 0;
        loadXML.RestoreDirectory = true;

        if(loadXML.ShowDialog() == DialogResult.OK)
        {
            try
            {
                if((input = loadXML.OpenFile()) != null)
                {
                    using(input)
                    {
                        XmlReader xmlFile = XmlReader.Create(input, new XmlReaderSettings());
                        DataSet dgvData = new DataSet();
                        dgvData.ReadXml(xmlFile);
                        dgvZenonXML.DataSource = dgvData.Tables[2];
                        this.checkedListBox1.DataSource = dgvData.Tables[0];
                        this.checkedListBox1.DisplayMember = "Name";
                        this.checkedListBox1.ValueMember = "Name";
                        xmlFile.Close();
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
        }
    }

At this point, I'm fairly stumped, as there isn't always a defined number of variables and data points available in each XML.

Aucun commentaire:

Enregistrer un commentaire