我首先尝试使用xmldatasource,发现您无法对其进行排序。如果您尝试的话,您只会收到错误:" system.notsupportedException:数据源不支持排序。"
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();
这将给我例外:"异常详细信息:system.web.httpexception:gridview'gridview1'触发事件分类,未处理。"
我的XML就是这样:
<Cars>
<car>
<id>11</id>
<make>Audi</make>
<model>A4</model>
<price>39000</price>
</car>
</Cars>
那么,解决这个问题的最佳方法是什么?还是我可以以某种方式处理排序事件?还是将XML数据加载到LINQ或类似的内容会更容易?
好吧,我为您找到了一些可能的答案。首先,如果要专门使用Xmldatasource,答案是使用XSLT文件来指定翻译,如此QA:
中如何正确地将XSLT参数传递给XMLDATASOURCE?
另外,这篇文章介绍了如何使用Xdocument将GridView绑定到LINQ查询的结果以加载XML文件:
http://forums.asp.net/t/1627778.aspx/1?sort results from xmldatasource