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

Sort GridTemplateColumn

1 Answer 133 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Jonathan
Top achievements
Rank 2
Jonathan asked on 09 Jan 2009, 01:25 AM
Hi guys,

I need help with sorting a customised GridTemplateColumn. The column snippet is shown below:

<telerik:GridTemplateColumn UniqueName="NameCommandColumn" AllowFiltering="False"  
        ItemStyle-Width="150" HeaderStyle-Width="150" ShowSortIcon="true" > 
            <HeaderTemplate>Name</HeaderTemplate> 
            <ItemTemplate>               
                <asp:HyperLink ID="hlnkContactName" runat="server"></asp:HyperLink>  
                <asp:Label ID="lblContactName" runat="server"></asp:Label> 
            </ItemTemplate>          
        </telerik:GridTemplateColumn>    

The column is used to display names either as a hyperlink or a simple label depending on the user's access rights. The main problem I have is that the values for these controls are generated at RadGrid's Item_Created event where it concatenates 2 strings (i.e. GivenNames & Surname) and binds the final output to the controls. See snippet below:

        protected void RadGrid1_ItemCreated(object sender, Telerik.Web.UI.GridItemEventArgs e) 
        {             
            if (e.Item is GridDataItem) 
            { 
                 
                //Check the user's permissions. if the user has edit permissions then allow edit.                 
                 
                if (SecurityHelper.IsAllowedTo("Modify","PhoneList")) 
                { 
                    #region Instantiate Name link 
                    HyperLink hlnkContactName = (HyperLink)e.Item.FindControl("hlnkContactName"); 
                    if (hlnkContactName != null
                    { 
                        Label lblContactName = (Label)e.Item.FindControl("lblContactName"); 
                        if (lblContactName != null
                            lblContactName.Visible = false
 
                        hlnkContactName.Attributes["href"] = "#"
                        hlnkContactName.Attributes["onclick"] = string.Format("return ShowForm('{0}')"
                            Constants.URL_PHONELISTEDITOR + "?" + Constants.QUERYSTRING_PARTYID + "=" + e.Item.OwnerTableView.DataKeyValues[e.Item.ItemIndex]["PartyId"].ToString()); 
                        hlnkContactName.Text = e.Item.OwnerTableView.DataKeyValues[e.Item.ItemIndex]["GivenNames"].ToString() +" "
                            e.Item.OwnerTableView.DataKeyValues[e.Item.ItemIndex]["Surname"].ToString();  
 
                    } 
                    #endregion 
                } 
                else 
                { 
                    #region Instantiate Name Label 
                    Label lblContactName = (Label)e.Item.FindControl("lblContactName"); 
                    if (lblContactName != null
                    { 
                        HyperLink hlnkContactName = (HyperLink)e.Item.FindControl("hlnkContactName"); 
                        if (hlnkContactName != null
                            hlnkContactName.Visible = false
 
                        lblContactName.Visible = true
                        lblContactName.Text = e.Item.OwnerTableView.DataKeyValues[e.Item.ItemIndex]["GivenNames"].ToString() +" "
                            e.Item.OwnerTableView.DataKeyValues[e.Item.ItemIndex]["Surname"].ToString();  
                    } 
                    #endregion                    
                }
          }
     } 

What I would like to do, is to be able to sort the column by its final output. but I'm not sure how to go about doing it. Please help!

Regards

Jonathan

1 Answer, 1 is accepted

Sort by
0
Shinu
Top achievements
Rank 2
answered on 09 Jan 2009, 08:29 AM
Hi Jonathan,

Instead of concatenating the two values in the ItemCreated event you can append those two fields(GivenNames and Surname) and return it as a single field in the Select query of the DataBase and bind the TemplateColumn with that field. Also set the SortExpression of the column.

Select Query:
"SELECT GivenNames, Surname, GivenNames + ' ' + Surname  AS NameCommandColumn FROM TableName"  

ASPX:
<telerik:GridTemplateColumn UniqueName="NameCommandColumn" DataField="NameCommandColumn" HeaderText="NameCommandColumn"  
                        SortExpression="NameCommandColumn" AllowFiltering="False"   
        ItemStyle-Width="150" HeaderStyle-Width="150" ShowSortIcon="true" >  
            <HeaderTemplate>Name</HeaderTemplate>  
            <ItemTemplate>                
                <asp:HyperLink ID="hlnkContactName" runat="server"></asp:HyperLink>   
                <asp:Label ID="lblContactName" runat="server"></asp:Label>  
            </ItemTemplate>           
        </telerik:GridTemplateColumn>   


Thanks
Shinu
Tags
Grid
Asked by
Jonathan
Top achievements
Rank 2
Answers by
Shinu
Top achievements
Rank 2
Share this question
or