开发者

How to Dynamically Bind XML to a WPF DataGrid in C#

开发者 https://www.devze.com 2023-01-20 20:30 出处:网络
I looked around for this, but all the examples I could find used XAML, which made the solution too static.Here is what I want to do:

I looked around for this, but all the examples I could find used XAML, which made the solution too static. Here is what I want to do:

I would like to populate a DataGrid's columns, rows and attributes from an XML file to be specified at run time. Nothing about the DataGrid's attributes can be fixed; the XML drives it down to the last detail (hence why the XAML examples I saw aren't sufficient).

The details of the XML file are open, so any layout will do, but as an example:

<data>
    <row Column="Col 1" Value="100" />
    <row Column="Col 2" Value ="200" />
</data>

Would yield a grid of 2 columns named Column & Value respectively with the values ("Col 1", 100) &开发者_StackOverflow中文版amp; ("Col 2", 200) for the row 1 & 2, respectively.

Again, I have no problem with radically different XML, so I'll take what works.

Something like this seems very useful as it would allow the creation of generic data viewing components in a variety of domains. XML would offer a convenient generic format for transmitting structured data and the DataGrid would offer a rich viewing experience.


Thank you to everyone who took the time to read or respond to my request. I figured out how to do this and am including a code snippet below:

using System.Xml.Linq;    // Required for XElement...
using System.Collections; // Required for Hashtable

private void InitGridFromXML(string xmlPath)
{
    var data = XElement.Load(xmlPath);

    // Set Grid data to row nodes (NOTE: grid = DataGrid member)
    var elements = data.Elements("row");
    grid.ItemsSource = elements;

    // Create grid columns from node attributes.  A hashtable ensures
    // only one column per attribute since this iterates through all
    // attributes in all nodes.  This way, the grid can handle nodes with
    // mutually different attribute sets.
    var cols = new Hashtable();
    foreach (var attr in elements.Attributes())
    {
        var col = attr.Name.LocalName;

        // Only add col if it wasn't added before
        if (!cols.Contains(col))
        {
            // Mark col as added
            cols[col] = true;

            // Add a column with the title of the attribute and bind to its
            // value
            grid.Columns.Add(new DataGridTextColumn
            {
                Header = col,
                Binding = new Binding("Attribute[" + col + "].Value")
            });
        }
    }
}


You could serialize the XML to a collection and bind that collection.

You could create a data table from your XML and bind that to the datagrid.

As long as you have data in master detail format it doesn't matter what structure you put it in.

The XML if you make like,

presents the data in rows and there columns values.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号