2

I first tried using xmldatasource and found out that you can't sort it. If you try you will just receive error: "System.NotSupportedException: The data source does not support sorting."

    GridView1.AllowSorting = true;
    DataSet carsDataSet;
    string filePath = Server.MapPath("App_Data/cars.xml");
    carsDataSet = new DataSet();
    //Read the contents of the XML file into the DataSet
    carsDataSet.ReadXml(filePath);
    GridView1.DataSource = carsDataSet.Tables[0].DefaultView;
    GridView1.DataBind();

This will give me exception: "Exception Details: System.Web.HttpException: The GridView 'GridView1' fired event Sorting which wasn't handled."

My xml is like this:

<Cars>
<car>
<id>11</id>
<make>Audi</make>
<model>A4</model>
<price>39000</price>
</car>
</Cars>

So what is the best way to get around this issue? Or can I handle the sorting event somehow? Or would it be easier to load xml data to LINQ or something like that?

Firze
  • 3,939
  • 6
  • 48
  • 61

1 Answers1

1

OK I found a couple possible answers for you. First, if you want to use XmlDataSource specifically, the answer is to use a XSLT file to specify translations as in this QA:

How do I pass a XSLT parameter to a XmlDataSource correctly?

Alternately, this post describes how to bind the GridView to the results of a LINQ query using XDocument to load the XML file:

http://forums.asp.net/t/1627778.aspx/1?Sort+results+from+XMLDataSource

Community
  • 1
  • 1
pseudocoder
  • 4,314
  • 2
  • 25
  • 40