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

Setting GridTemplateColumn Sortexpression dynamically

4 Answers 112 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Roy Halvorsen
Top achievements
Rank 1
Roy Halvorsen asked on 27 Oct 2008, 02:38 PM
I have a grid that uses XMLDataSource. Since the XML file is pretty complex, i have to use GridColumnTemplate and set XPath in the ItemTemplate as follows:
<telerik:GridTemplateColumn UniqueName="Klasse" HeaderText="Klasse">  
  <ItemTemplate> 
    <asp:Label ID="lblKlasse" runat="server" Text='<%# XPath("Klasse") %>' /> 
  </ItemTemplate> 
</telerik:GridTemplateColumn> 
<telerik:GridTemplateColumn UniqueName="Orden" HeaderText="Orden">  
  <ItemTemplate> 
    <asp:Label ID="lblOrden" runat="server" Text='<%# XPath("Orden") %>' /> 
  </ItemTemplate> 
</telerik:GridTemplateColumn> 

How can I access each GridTemplateColumn in codebehind (i.e. ItemDataBound) to set SortExpression to some XPath?

4 Answers, 1 is accepted

Sort by
0
Shinu
Top achievements
Rank 2
answered on 28 Oct 2008, 03:47 AM
Hi Roy,

Try the following code snippet to access the GridTemplateColumn in the PreRender event.

CS:
 protected void RadGrid1_ItemDataBound(object sender, Telerik.Web.UI.GridItemEventArgs e) 
    { 
        foreach (GridColumn col in RadGrid1.MasterTableView.RenderColumns) 
        { 
            if ((col.ColumnType == "GridTemplateColumn") && (col.UniqueName == "TempCol")) 
            { 
                GridTemplateColumn TempCol = (GridTemplateColumn)col; 
                TempCol.SortExpression = "ProductName"
                
            } 
        } 
   } 

Thanks
Shinu.
0
Roy Halvorsen
Top achievements
Rank 1
answered on 28 Oct 2008, 07:34 AM
Thanks a lot. It nearly works. Now I am able to click on the headertext, but I get the error Sys.WebForms.PageRequestManagerServerErrorException: Cannot find column ProductName. I also tried to put your code solution in the RadGrid1_SortCommand event, but with the same result.

Roy
0
Princy
Top achievements
Rank 2
answered on 28 Oct 2008, 11:36 AM
Hi Roy,

When using GridTemplateColumn, the SortExpression value should match the data field you want to sort on (typically the field a control in the template is bound to).
Sorting for GridHyperLinkColumn/GridTemplateColumn

Princy.

0
Roy Halvorsen
Top achievements
Rank 1
answered on 28 Oct 2008, 11:39 AM
So what you're telling me is that I cannot use SortExpression when using XmlDataSource and XPath?
Tags
Grid
Asked by
Roy Halvorsen
Top achievements
Rank 1
Answers by
Shinu
Top achievements
Rank 2
Roy Halvorsen
Top achievements
Rank 1
Princy
Top achievements
Rank 2
Share this question
or