Home Forums WPF controls Xceed DataGrid for WPF Dynamic Data Binding in Code Behind

Viewing 6 posts - 1 through 6 (of 6 total)
  • Author
    Posts
  • User (Old forums)
    Member
    Post count: 23064
    #22076 |

    I would like to dynamically bind xml data to an xceed grid.
    Both the xml data and the xaml to instantiate the grid are created at runtime.
    In addition, the schema for the xml data is not known until runtime.
    I would like to keep the xaml definition completely separate from the data.
    As I understand it, I must therefore handle the binding in the code behind rather than using a resource embedded into the XAML. (I had started down this path but realized it is not correct.)

    Here is the xaml containing an xceed grid definition:
    ————————————————————————–

    <Page xmlns=”http://schemas.microsoft.com/winfx/2006/xaml/presentation&#8221;
    xmlns:x=”http://schemas.microsoft.com/winfx/2006/xaml&#8221;
    xmlns:s=”clr-namespace:System;assembly=mscorlib” xmlns:xcdg=”http://schemas.xceed.com/wpf/xaml/datagrid”&gt;
    <xcdg:DataGridControl Name=”Query”>
    <xcdg:DataGridControl.Columns>
    <xcdg:Column FieldName=”account.name” Title=”Account” Width=”30″ DisplayMemberBinding=”{Binding XPath=account.name}” />
    <xcdg:Column FieldName=”account.type” Title=”Type” Width=”100.00000005″ DisplayMemberBinding=”{Binding XPath=account.type}” />
    <xcdg:Column FieldName=”account.salesrep” Title=”Account Manager” Width=”125.0000000625″ DisplayMemberBinding=”{Binding XPath=account.salesrep}” />
    <xcdg:Column FieldName=”account.phone” Title=”Phone” Width=”100.00000005″ DisplayMemberBinding=”{Binding XPath=account.phone}” />
    <xcdg:Column FieldName=”account.ext” Title=”Ext.” Width=”54.000000027″ DisplayMemberBinding=”{Binding XPath=account.ext}” />
    <xcdg:Column FieldName=”account.account_id” Title=”ID” Width=”101.0000000505″ DisplayMemberBinding=”{Binding XPath=account.account_id}” />
    </xcdg:DataGridControl.Columns>
    </xcdg:DataGridControl>
    </Page>

    Here is the xml containing the data to be bound:
    ———————————————————————
    <EntGridRowData xmlns:xsi=”http://www.w3.org/2001/XMLSchema-instance&#8221; xmlns:xsd=”http://www.w3.org/2001/XMLSchema”&gt;
    <Row>
    <account.name>Aachen Automotive</account.name>
    <account.type>Business</account.type>
    <account.salesrep>GRAFTON</account.salesrep>
    <account.phone>+1-910-77039</account.phone>
    <account.ext>0</account.ext>
    <account.account_id>7</account.account_id>
    </Row>
    <Row>
    <account.name>Bank of Mideast Pacific</account.name>
    <account.type>Business</account.type>
    <account.salesrep>GRAFTON</account.salesrep>
    <account.phone>+1-307-4789395</account.phone>
    <account.ext>0</account.ext>
    <account.account_id>5</account.account_id>
    </Row>
    </EntGridRowData>

    After the xaml for the datagrid is created (by a webservice) and received by the client either as a string or as an XmlDocument (called objDocument in the example below), the client will instantiate the xaml like this:

    EntData queryDef = QueryGetData(queryName); //special format of query definition plus data
    XmlDocument objDocument = QueryRender(queryDef, @”C:\Ent10\Graphics\”); //get the definition
    XmlDocument objData = QueryPopulate(queryDef, objDocument); //get the data

    object obj_one = null;
    object nodeTemp = null;
    object nodeTemp1 = null;

    // get XAML code in a big string
    String strXAML = objDocument.OuterXml;
    //now take the string and turn it into a stream
    StringReader strReader = new StringReader(strXAML);
    XmlTextReader xmlReader = new XmlTextReader(strReader);
    obj_one = XamlReader.Load(xmlReader);

    //render the stream
    nodeTemp = contentTabs.FindName(“contentFrame”);
    if (nodeTemp is Frame)
    {
    ((Frame)nodeTemp).Content = obj_one;
    }

    Once this code runs, the grid is rendered and I can use further code to manipulate the object. So, I would like to take the xml data, make a binding, and assign the binding to the grid. Then, voila, I expect to see the data appear. Here is the code I am trying to have do this (obj_one is the page control that contains my xceed grid…see the xaml above):

    if (obj_one is Page)
    {
    Page myPage = (Page)obj_one;
    nodeTemp1 = myPage.FindName(“Query”);
    if (nodeTemp1 is DataGridControl)
    {
    DataGridControl myQueryGrid = (DataGridControl)nodeTemp1;
    Binding myBinding = new Binding();
    myBinding.Source = objData;
    myBinding.Mode = BindingMode.OneWay;
    myQueryGrid.SetBinding(DataGridControl.ItemsSourceProperty, myBinding);

    // this seems to do the same thing
    myQueryGrid.ItemsSource = objData;
    }
    }

    }

    If new data must replace existing data in the grid, I should not have to re-instantiate any part of the UI simply to refresh a resource section; rather, I should be able to receive a new XmlDocument or string containing the new data, and re-bind the new data to the grid.

    Can you tell me if my thinking about how to do this is correct and also maybe you can point out what I have done wrong here? The code compiles and runs with no error, but what appears in the grid is only one empty row. (Actually my xml data has seven rows but I only showed two above for brevity.)

    (Please pardon my sloppy code :-$; I’m trying all kinds of different things.)

    Thanks for your help.

    Imported from legacy forums. Posted by Marty (had 4762 views)

    User (Old forums)
    Member
    Post count: 23064

    Hi,

    You need to specify the XPath to the data in your Binding:

    <b>myBinding.XPath = “EntGridRowData/Row”;</b>

    Hope this helps!

    Imported from legacy forums. Posted by Pascal (had 393 views)

    User (Old forums)
    Member
    Post count: 23064

    Awesome!
    Thanks for you help.

    Imported from legacy forums. Posted by Marty (had 872 views)

    User (Old forums)
    Member
    Post count: 23064

    By the way, sorting works fine.
    Click on a column heading and the rows sort appropriately.
    I did not expect this because I have only the first release, which I thought has a known issue where xml bound data could not be sorted.

    Imported from legacy forums. Posted by Marty (had 671 views)

    User (Old forums)
    Member
    Post count: 23064

    If you only have a few DataRows (so that the vertical scroll bar is disabled), the problem that was fixed in the 2nd release might not manifest itself.

    Imported from legacy forums. Posted by Pascal (had 4281 views)

    User (Old forums)
    Member
    Post count: 23064

    I would create a dataset to store the incoming xml data.  If you then bind the grid to a DataTable, the datatable can be changed in code at any time and the grid will display (and edit) the current state of the table.  I know everyone always wants to create objects that represent each row of data, but all that work is already done with a DataSet.  All you have to do is make a property or method that returns the datatable and run the following code to bind it to the grid:

    Binding myBinding = new Binding();
    myBinding.Source = GetTable();  // method that returns datatable
    Grid1.SetBinding(DataGridControl.ItemsSourceProperty, myBinding);

    That’s it.

    Imported from legacy forums. Posted by Toby (had 1170 views)

Viewing 6 posts - 1 through 6 (of 6 total)
  • You must be logged in to reply to this topic.