views:

3322

answers:

4

In their documentation Telerik says that there is a way to disable sorting for specific column by using AllowSorting property. I'm looking at Telerik.Web.UI.GridColumn members and there is no AllowSorting property. There is a Sortable property but its protected and has to be overriden. So what do I use to turn off sorting for specific column?

There is a AllowSorting property on GridBoundColumn but in this case I'm using GridTemplateColumn. Is there a way to turn off sorting on GridTemplateColumn?

A: 

Here's an example showing how to disable sorting for a specific column.

Note the AllowSorting property at the Grid level (for all columns).

Then, in the Columns collection, note how it is turned off for that specific column.

<telerik:RadGrid ID="RadGrid1" runat="server" AllowSorting="True">
    <HeaderContextMenu>
        <CollapseAnimation Duration="200" Type="OutQuint" />
    </HeaderContextMenu>
    <MasterTableView>
        <RowIndicatorColumn>
            <HeaderStyle Width="20px" />
        </RowIndicatorColumn>
        <ExpandCollapseColumn>
            <HeaderStyle Width="20px" />
        </ExpandCollapseColumn>
        <Columns>
            <telerik:GridBoundColumn AllowSorting="False" UniqueName="column">
            </telerik:GridBoundColumn>
        </Columns>
    </MasterTableView>
    <FilterMenu>
        <CollapseAnimation Duration="200" Type="OutQuint" />
    </FilterMenu>
</telerik:RadGrid>

For TemplateColumns, I would try turning off Sorting at the grid level and simply enable it on the columns needed. That way, you won't have to do anything for the TemplateColumn since it will be disabled by default.

Nebakanezer
Yes, there is a AllowSorting property on GridBoundColumn but in this case I'm using GridTemplateColumn. Is there a way to turn off sorting on GridTemplateColumn?
dev.e.loper
Yeah that would work but there is no enable/disable sorting property for GridTemplateColumn.
dev.e.loper
+2  A: 

The AllowSorting attribute is available from the source/markup view in Visual Studio. For example (simplified):

    <tr:RadGrid>
    <MasterTableView>
        <Columns>
            <tr:GridBoundColumn DataField="field" HeaderText="Description" 
                 AllowSorting="false" />
        </Columns>
    </MasterTableView>
    </tr:RadGrid>

I don't know if this works for you? I haven't instantiated my grids from the code-behind files yet, so if that's what you are doing, I can't easily help you. But the above works for me.


EDIT:

Ah it was not clear from the original question, that you were using the GridTemplate column. As you are now using the SortExpression-property, doesn't using the same attribute in the markup work for you? So:

    <tr:RadGrid>
    <MasterTableView>
        <Columns>
            <tr:GridTemplateColumn HeaderText="Description" DataField="field" 
                SortExpression="">
                <!-- template here etc. -->
            </tr:GridTemplateColumn>
        </Columns>
    </MasterTableView>
    </tr:RadGrid>
Cloud
Yes, there is a AllowSorting property on GridBoundColumn but in this case I'm using GridTemplateColumn. Is there a way to turn off sorting on GridTemplateColumn?
dev.e.loper
This way is easier than the below codebehind
Sameer Alibhai
A: 

Okay, I got the desired effect, I turned off sorting by setting GridTemplateColumn's SortingExpression property to blank.

Grid.Columns.FindByUniqueName("Type").SortExpression = string.Empty;

This looks like a hack. Why isn't there an explicit property to turn off sorting? Oh well. This works.

If you know a better way, let me know.

Thanks.

dev.e.loper
I have edited an extra sample in my original post. It's based on what you are doing here. Does that work for you?
Cloud
Yes. Basically we are doing same thing. Only I'm doing it in code-behind and you are doing it declaritevely.
dev.e.loper
Dont know why they just didnt provide the AllowSorting, but dont think its a hack, at least for client side binding this totally disables the sort behaviour.
Sameer Alibhai
A: 

You could always supply your own headertemplate with a label as the header instead of a link button if you are using a GridTemplateColumn. A we bit extra work but this works fine. If you are going to disable sorting for all your GridTemplateColumns then your "hack" would be best.

infocyde