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

[Solved] Cannot sort a template column

2 Answers 157 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Clive Hoggar
Top achievements
Rank 1
Clive Hoggar asked on 22 Oct 2009, 07:04 PM
Hi

Can you give me some help on this... I am struggling.

I have a column on which the column sort does not work,
<telerik:GridTemplateColumn DataField="PricePerBottle"   
DataType="System.Decimal" HeaderText="£/Bottle"   
SortExpression="PricePerBottle" UniqueName="PricePerBottle">  
    <ItemTemplate> 
        <asp:Label ID="PricePerBottleLabel" runat="server"   
        Text='<%# ConstClass.getCurrencyPrice(Eval("PricePerBottle"))%'>  
        </asp:Label> 
    </ItemTemplate> 
    <HeaderStyle HorizontalAlign="Right" Width="70px" /> 
    <ItemStyle HorizontalAlign="Right" /> 
</telerik:GridTemplateColumn> 
It all looks OK to me but maybe the code behind is interfering, because I change the HeaderText in the ItemDataBound event.
Could that be an issue?  Can I get round it?
Thanks for your help!

Clive

2 Answers, 1 is accepted

Sort by
0
Princy
Top achievements
Rank 2
answered on 23 Oct 2009, 06:37 AM
Hello Clive,

When sorting is enabled in the grid, and if you want to change the header text for a column, you would have to access the linkbutton in the header and then set its text as shown below. When you set the text of the cell directly sorting gets disabled.
c#:
protected void RadGrid1_ItemDataBound(object sender, Telerik.Web.UI.GridItemEventArgs e) 
    { 
        if (e.Item is GridHeaderItem) 
        { 
            GridHeaderItem item = (GridHeaderItem)e.Item;             
            ((LinkButton)item["PricePerBottle"].Controls[0]).Text = "CustomText"
        } 
    } 

Hope this helps..
Princy.
0
Clive Hoggar
Top achievements
Rank 1
answered on 23 Oct 2009, 05:01 PM
Hi Prinzy

Thanks a lot for this. I had it almost right but changed it to your method in line 3 and all is OK now.

Just one item of feedback that may help someone..

I used the Telerik converter to get VB and it gave me code below. I got an intellisense error and runtime syntax error for the opening bracket and corresponding closing bracket in the third line. After removal of those, all was fine. Maybe the Converter needs a tweak?

Thanks for your great support, as always,

Clive

If TypeOf e.Item Is GridHeaderItem Then 
        Dim item As GridHeaderItem = DirectCast(e.Item, GridHeaderItem) 
        (DirectCast(item("PricePerBottle").Controls(0), LinkButton)).Text = "CustomText" 
    End If 
Tags
Grid
Asked by
Clive Hoggar
Top achievements
Rank 1
Answers by
Princy
Top achievements
Rank 2
Clive Hoggar
Top achievements
Rank 1
Share this question
or