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

Grouping on XmlDataSource column

1 Answer 106 Views
Grid
This is a migrated thread and some comments may be shown as answers.
TonyG
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:
FieldName="XPath("pubDate").ToString().Substring(0,3)" 
 
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#)

Thanks!!


<telerik:RadGrid ID="RadGrid1" runat="server" AutoGenerateColumns="False" DataSourceID="RSS1" 
  GridLines="None" AllowSorting="True" ShowGroupPanel="True">  
  <HeaderContextMenu EnableAutoScroll="True">  
  </HeaderContextMenu> 
  <MasterTableView DataSourceID="RSS1" GroupLoadMode="Client">  
    <GroupByExpressions> 
      <telerik:GridGroupByExpression> 
        <SelectFields> 
          <telerik:GridGroupByField FieldName="pubDate" HeaderText="Day" /> 
        </SelectFields> 
        <GroupByFields> 
          <telerik:GridGroupByField  FieldName="pubDate" SortOrder="Ascending" /> 
        </GroupByFields> 
      </telerik:GridGroupByExpression> 
    </GroupByExpressions> 
    <RowIndicatorColumn> 
      <HeaderStyle Width="20px"></HeaderStyle> 
    </RowIndicatorColumn> 
    <ExpandCollapseColumn> 
      <HeaderStyle Width="20px"></HeaderStyle> 
    </ExpandCollapseColumn> 
    <Columns> 
      <telerik:GridTemplateColumn UniqueName="c1" Groupable="false" HeaderText="Title" 
        ReadOnly="True">  
        <ItemTemplate> 
          <target="_blank" href='<%# XPath("link") %>'>  
            <%# System.Web.HttpUtility.HtmlEncode(XPath("title").ToString())%> 
          </a> 
        </ItemTemplate> 
      </telerik:GridTemplateColumn> 
      <telerik:GridTemplateColumn UniqueName="c2" Groupable="true" HeaderText="Day" 
        ReadOnly="True">  
        <ItemTemplate> 
          <%# System.Web.HttpUtility.HtmlEncode(XPath("pubDate").ToString().Substring(0,3))%> 
        </ItemTemplate> 
      </telerik:GridTemplateColumn> 
      <telerik:GridTemplateColumn UniqueName="c3" Groupable="false" HeaderText="Description" 
        ReadOnly="True">  
        <ItemTemplate> 
          <%# System.Web.HttpUtility.HtmlEncode(XPath("description").ToString())%> 
        </ItemTemplate> 
      </telerik:GridTemplateColumn> 
    </Columns> 
  </MasterTableView> 
  <ClientSettings AllowColumnsReorder="true" AllowDragToGroup="true" /> 
</telerik:RadGrid> 
<asp:XmlDataSource ID="RSS1" runat="server" DataFile="http://mydomain.com/blog/feed" 
  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="http://www.w3.org/1999/XSL/Transform" 
    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="."/>  
        </xsl:attribute> 
      </xsl:for-each> 
      <xsl:apply-templates/> 
    </xsl:element> 
  </xsl:template> 
</xsl:stylesheet> 

1 Answer, 1 is accepted

Sort by
0
Yavor
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.

Greetings,
Yavor
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.
Tags
Grid
Asked by
TonyG
Top achievements
Rank 1
Answers by
Yavor
Telerik team
Share this question
or