locked
Sorting a GridView bound to XmlDataSource - possible? RRS feed

  • Question

  • User-1328661006 posted
    Hello.

    I have a FormView that is bound to an XmlDataSource and contains a GridView that displays dates and their types. The data is loaded into the XmlDataSource from an XML server and looks like this:

    <conference>
    <title>blah</title>
    <date type="registration-deadline">2007-03-15</date>
    <date type="start-date">2007-04-01</date>
    <date type="today">2007-01-07</date>
    ...
    </conference>

    I want the table to be sorted chronologically. I cannot do that on the server (I think), as the type="today" date is added after transforming the date nodes. I also can't use builtin sorting capabilities of GridView as (accoring to MSDN) XmlDataSource data cannot be sorted this way.
    Is there any other method? Can I use XPathExpression with AddSort instead of XPathSelect binder? Or do I have to write a transform file from my XmlDataSource?

    Sunday, January 7, 2007 7:06 AM

Answers

  • User1335583151 posted

    Hello Onodera my friend, I have the answer for you. 

    First you bind the xml document to a dataset. 

    Then get the DataView from the DataTable within the DataSet: -

    System.Data.DataView dv = new System.Data.DataView(MyDataSet.Tables[0]);

    Then sort the DataView: -

    dv.Sort = "today DESC"

    Then bind the GridView to the DataView: -

    gridView1.DataSource = dv;

    gridView1.DataBind();

     

    Kind regards

    Scotty

     

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Sunday, January 7, 2007 5:52 PM

All replies

  • User1335583151 posted

    Hello Onodera my friend, I have the answer for you. 

    First you bind the xml document to a dataset. 

    Then get the DataView from the DataTable within the DataSet: -

    System.Data.DataView dv = new System.Data.DataView(MyDataSet.Tables[0]);

    Then sort the DataView: -

    dv.Sort = "today DESC"

    Then bind the GridView to the DataView: -

    gridView1.DataSource = dv;

    gridView1.DataBind();

     

    Kind regards

    Scotty

     

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Sunday, January 7, 2007 5:52 PM
  • User-512755392 posted

    Hi

    Sort the DataView as suggested by ask_Scotty  is a simple and good way.

    If  you are interested in XSLT whose  xsl:sort instruction lets you sort a group of similar elements here is a tutorial:

    Sorting in XSLT

    Monday, January 8, 2007 12:06 AM