Welcome to the Xceed Community Sign in | Join | Help
Community Search  

DataGrid Binding XML with LINQ

Sort Posts: Previous Next
  •  05-03-2008, 12:45 PM Post no. 11947

    DataGrid Binding XML with LINQ

    Hello,

    I wondered if somebody could write an example for Binding XML to a DataGrid with LINQ.

    I'm new to all this stuff and I would really appreciate this. Please, if someone does this I'd like to have the whole Project not just code snippets, I think you will test it anyway and have a project then.

    I would be deeply grateful.

     

    ebber
     

  •  05-03-2008, 1:36 PM Post no. 11949 in reply to 11947

    Re: DataGrid Binding XML with LINQ

    The "Binding to a LINQ query (XML) example in the documentation should help you get started:

    <Grid xmlns:xcdg="http://schemas.xceed.com/wpf/xaml/datagrid">
      <Grid.Resources>

        <xcdg:DataGridCollectionViewSource x:Key="cvs_orders"
                        
    Source="{Binding RelativeSource={RelativeSource AncestorType={x:Type Window}},
                                         
    Path=XmlData}"/>

      </Grid.Resources>

     

      <xcdg:DataGridControl x:Name="OrdersGrid"
                           
    ItemsSource="{Binding Source={StaticResource cvs_orders}}"/>

    </Grid>

     CODE_BEHIND

    public IEnumerable XmlData
    {
     get
     {
       XDocument document = App.NorthwindDocument;

       var data = from order
    in document.Element( "dataroot" ).Descendants( "Orders" )
                  select
    new
                  {
                    
    ShipCountry = order.Element( "ShipCountry" ).Value,
                    ShipCity = order.Element(
    "ShipCity" ).Value,
                    ShipAddress = order.Element(
    "ShipAddress" ).Value,
                    ShipName = order.Element(
    "ShipName" ).Value,
                    ShipVia = order.Element(
    "ShipVia" ).Value,
                    Freight = order.Element(
    "Freight" ).Value
                  };
       
    return data.ToList();
     }
    }

     


    Technical Writer - Xceed Software

    Of all the things I've lost, I miss my mind the most. - Mark Twain
    Filed under: ,
  •  05-03-2008, 5:50 PM Post no. 11950 in reply to 11949

    Re: DataGrid Binding XML with LINQ

     Hi,

     
    I thought something like this would happen. Now I have no idea where to put this lower part of the code. May you help me in this next small step?

     

    ebber
     

  •  05-05-2008, 7:59 AM Post no. 11959 in reply to 11950

    Re: DataGrid Binding XML with LINQ

    The Source property of the DataGridCollectionView is bound to the XmlData property, which according to the binding is located in the parent Window:

    Source="{Binding RelativeSource={RelativeSource AncestorType={x:Type Window}}, Path=XmlData}"/>

    That said, the XmlData property should be placed in your Window that corresponds to the XAML code-behind page. If you are using a Page (or any other container), then  you can simply change the binding accordingly.


    Technical Writer - Xceed Software

    Of all the things I've lost, I miss my mind the most. - Mark Twain
  •  05-05-2008, 11:27 AM Post no. 11979 in reply to 11959

    Re: DataGrid Binding XML with LINQ

    Now, I don't know more than before. Maybe today is not my day.

     

    I'll tell you again what I want. Maybe then it all will become clear, to me or to you.

    I don't know where to place this code :     public IEnumerable XmlData......

    If I put it in the code file that goes with my window I get this error: 

     Error    1    The type or namespace name 'IEnumerable' could not be found (are you missing a using directive or an assembly reference?)

    Would you help me again?

     

    ebber

     

  •  05-05-2008, 11:31 AM Post no. 11980 in reply to 11979

    Re: DataGrid Binding XML with LINQ

    Add a using directive to : System.Collections  (using System.Collections;)

    or use the complete qualified name for IEnumerable : System.Collections.IEnumerable XmlData...


    Technical Writer - Xceed Software

    Of all the things I've lost, I miss my mind the most. - Mark Twain
  •  05-05-2008, 12:14 PM Post no. 11982 in reply to 11980

    Re: DataGrid Binding XML with LINQ

    Thanks. This works now.

    But of course there is a new problem:   XDocument document = App.NorthwindDocument;

    What is a XDocument? I read in the Help but there was nothing useful. I assume it is some kind of XML Document and the problem is small like the last one.

    I made this so you can say immediately that it is wrong, so I won't have to ask again:

     <?xml version="1.0" encoding="utf-8" ?>
    <dataroot>
      <Orders ShipCountry="a" ShipCity="a" ShipAddress="a" ShipName="a" ShipVia="a" Freight="a" />
      <Orders ShipCountry="b" ShipCity="b" ShipAddress="b" ShipName="b" ShipVia="b" Freight="b" />
    </dataroot>
     

  •  05-05-2008, 4:01 PM Post no. 11988 in reply to 11982

    Re: DataGrid Binding XML with LINQ

    Hi ebber,

        These questions are more relevant to getting started with Linq to XML rather than the Xceed DataGrid for WPF.

        I suggest going to the MSDN Linq to XML web-site and forum if you need further assistance getting started with Linq to XML.

       Linq to Xml Web Site

    Marc Laroche
    Software Developer
    Xceed Software Inc.


    I don’t suffer from insanity, I enjoy every minute of it. - Unknown
  •  05-05-2008, 4:40 PM Post no. 11990 in reply to 11988

    Re: DataGrid Binding XML with LINQ

    Hi,

     I think you are right. I'm sorry.

     ebber
     

    ps: I found my problem, it is very misterious. The System.Xml.Linq-Namespace is missing, which contains the XDocument. I know this is the wrong place to ask this but if someone has a clue he may write me a pm.

     

  •  05-05-2008, 5:20 PM Post no. 11992 in reply to 11990

    Re: DataGrid Binding XML with LINQ

    You need to add the LINQ assembly to your references: System.Xml.Linq.dll

     


    Technical Writer - Xceed Software

    Of all the things I've lost, I miss my mind the most. - Mark Twain
  •  05-06-2008, 10:14 AM Post no. 12015 in reply to 11992

    Re: DataGrid Binding XML with LINQ

    Thanks, it's working now, not all but this one.

    I can't find a mistake but it doesn't want to load the data. I made new coloums with the filednames ShipCountry....

    Either my XML file is wrong :

     <?xml version="1.0" encoding="utf-8" ?>
    <dataroot>
      <Orders ShipCountry="a" ShipCity="a" ShipAddress="a" ShipName="a" ShipVia="a" Freight="a" />
      <Orders ShipCountry="b" ShipCity="b" ShipAddress="b" ShipName="b" ShipVia="b" Freight="b" />
    </dataroot>

    or maybe I have to do something with the Grid first? 

  •  07-22-2008, 5:13 PM Post no. 13689 in reply to 11959

    Re: DataGrid Binding XML with LINQ

    Jenny:

    The Source property of the DataGridCollectionView is bound to the XmlData property, which according to the binding is located in the parent Window:

    Source="{Binding RelativeSource={RelativeSource AncestorType={x:Type Window}}, Path=XmlData}"/>

    That said, the XmlData property should be placed in your Window that corresponds to the XAML code-behind page. If you are using a Page (or any other container), then  you can simply change the binding accordingly.

     Hi There

    I am working on the same example however VS2008 keeps telling me that in order to use AncestorType I have to have a Mode.  I have added the Mode=FindAncestor, in front of the AncestorType attrib but now VS2008 is telling me I need to define the AncestorType, but its right there???

     

    Anyone got any suggestions as to why this snippet of code is throwing these errors up in VS 2008?

     

    Cheers

    Paul 

View as RSS news feed in XML
Contact | Site Map | Reviews | Legal Terms of Use | Trademarks | Privacy Statement Copyright 2008 Xceed Software Inc.