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#)
Thanks!!
Here is the XSL which removes namespace info:
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)" |
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> |
<a 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> |