This is a migrated thread and some comments may be shown as answers.

Grouping on XmlDataSource column

1 Answer 73 Views
This is a migrated thread and some comments may be shown as answers.
Top achievements
Rank 1
TonyG asked on 18 Mar 2010, 08:28 PM
I've created a simple grid to display a couple columns from an RSS feed using an XmlDataSource. The columns display fine but I want to group by a substring of one of the columns. The code for the grid is below (I hope this is educational for anyone else who wants to do this).

In short, there are three columns, c1, c2, and c3. These show RSS data 'title', 'pubDate', and 'description'. Rather than showing the whole publication date, for this example I just want to extract the first three characters of the day of the week so that I can group on it.

This isn't working with database columns, it's using XPath(expression) to render nodes as columns. So the GroupByField FieldName references below are invalid. Therefore, at runtime this error displays:
Telerik.Web.UI.GridGroupByException: Field pubDate not found in the source table. Please check the expression syntax.

It seems that the FieldName must actually be a database field name, not a column name from the grid. I've tried using the alias syntax but haven't been successful. I don't want to just use the fieldname, I want to group on a substring of that field, so it seems like the GroupByExpression needs to be something like this:
But because of the quotes that's not going to work.

So can this be accomplished in markup? If not, how would it be done in code? (I tend toward C#)


<telerik:RadGrid ID="RadGrid1" runat="server" AutoGenerateColumns="False" DataSourceID="RSS1" 
  GridLines="None" AllowSorting="True" ShowGroupPanel="True">  
  <HeaderContextMenu EnableAutoScroll="True">  
  <MasterTableView DataSourceID="RSS1" GroupLoadMode="Client">  
          <telerik:GridGroupByField FieldName="pubDate" HeaderText="Day" /> 
          <telerik:GridGroupByField  FieldName="pubDate" SortOrder="Ascending" /> 
      <HeaderStyle Width="20px"></HeaderStyle> 
      <HeaderStyle Width="20px"></HeaderStyle> 
      <telerik:GridTemplateColumn UniqueName="c1" Groupable="false" HeaderText="Title" 
          <target="_blank" href='<%# XPath("link") %>'>  
            <%# System.Web.HttpUtility.HtmlEncode(XPath("title").ToString())%> 
      <telerik:GridTemplateColumn UniqueName="c2" Groupable="true" HeaderText="Day" 
          <%# System.Web.HttpUtility.HtmlEncode(XPath("pubDate").ToString().Substring(0,3))%> 
      <telerik:GridTemplateColumn UniqueName="c3" Groupable="false" HeaderText="Description" 
          <%# System.Web.HttpUtility.HtmlEncode(XPath("description").ToString())%> 
  <ClientSettings AllowColumnsReorder="true" AllowDragToGroup="true" /> 
<asp:XmlDataSource ID="RSS1" runat="server" DataFile="" 
  TransformFile="~/XSL.xsl" XPath="rss/channel/item"></asp:XmlDataSource> 

Here is the XSL which removes namespace info:
<?xml version="1.0" encoding="utf-8"?>  
<xsl:stylesheet version="1.0" xmlns:xsl="" 
    xmlns:msxsl="urn:schemas-microsoft-com:xslt" exclude-result-prefixes="msxsl">  
  <xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>  
  <xsl:template match="*">  
    <!-- Remove any prefixes --> 
    <xsl:element name="{local-name()}">  
      <!-- Work through attributes --> 
      <xsl:for-each select="@*">  
        <!-- Remove any attribute prefixes --> 
        <xsl:attribute name="{local-name()}">  
          <xsl:value-of select="."/>  

1 Answer, 1 is accepted

Sort by
Telerik team
answered on 24 Mar 2010, 07:40 AM
Hello TonyG,

Indeed, your observation is correct. In order for the grouping/filtering/sorting to work as expected, the field must be part of the datasource of the control. One possible option in this case is as follows. You can use the NeedDataSource event handler to populate the grid with data. From the code-behind, you can read the data from the xml, and prepare a datatable, with all the data. You can then pass this data as a datasource for the grid.
I hope this suggestion helps.

the Telerik team

Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items.
Asked by
Top achievements
Rank 1
Answers by
Telerik team
Share this question