【问题标题】:Best way to sort Gridview using xmldatasource?使用 xmldatasource 对 Gridview 进行排序的最佳方法?
【发布时间】:2012-10-30 13:32:23
【问题描述】:

我第一次尝试使用 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 或类似的东西会更容易吗?

【问题讨论】:

    标签: asp.net .net gridview xmldatasource


    【解决方案1】:

    好的,我为您找到了几个可能的答案。首先,如果您想专门使用 XmlDataSource,答案是使用 XSLT 文件来指定翻译,如本 QA:

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

    另外,这篇文章描述了如何使用 XDocument 将 GridView 绑定到 LINQ 查询的结果以加载 XML 文件:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-11-20
      • 2010-09-07
      • 1970-01-01
      • 2017-05-23
      • 2021-01-04
      • 2011-01-24
      • 2016-04-07
      相关资源
      最近更新 更多