Setting GridTemplateColumn Sortexpression dynamically

5 posts, 0 answers
  1. Roy Halvorsen
    Roy Halvorsen avatar
    83 posts
    Member since:
    Oct 2008

    Posted 27 Oct 2008 Link to this post

    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?
  2. Shinu
    Shinu avatar
    17764 posts
    Member since:
    Mar 2007

    Posted 27 Oct 2008 Link to this post

    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.
  3. UI for ASP.NET Ajax is Ready for VS 2017
  4. Roy Halvorsen
    Roy Halvorsen avatar
    83 posts
    Member since:
    Oct 2008

    Posted 28 Oct 2008 Link to this post

    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
  5. Princy
    Princy avatar
    17421 posts
    Member since:
    Mar 2007

    Posted 28 Oct 2008 Link to this post

    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.

  6. Roy Halvorsen
    Roy Halvorsen avatar
    83 posts
    Member since:
    Oct 2008

    Posted 28 Oct 2008 Link to this post

    So what you're telling me is that I cannot use SortExpression when using XmlDataSource and XPath?
Back to Top
UI for ASP.NET Ajax is Ready for VS 2017