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

Default Datetime field sorting in Descending order is not working in Radgrid

8 Answers 377 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Mandy
Top achievements
Rank 2
Mandy asked on 18 Aug 2010, 12:49 PM
i am trying with both gridboundColumn and with gridDatetime column but its is not working for any of ane.below is my code for sorting .
Please let me know if i had done any mistake in this ASAP.


<telerik:RadGrid ID="gvMessages" runat="server" AllowSorting="true" ShowGroupPanel="true"
                                                        PageSize="20" AllowPaging="True" AutoGenerateColumns="False" GridLines="Horizontal"
                                                        OnItemDataBound="gvMessages_ItemDataBound" OnNeedDataSource="gvMessages_NeedDataSource"
                                                        OnGroupsChanging="gvMessages_GroupsChanging" OnItemCreated="gvMessages_ItemCreated"
                                                        OnItemCommand="gvMessages_ItemCommand" AllowMultiRowSelection="true" OnRowDrop="gvMessages_RowDrop">
                                                        <HeaderContextMenu EnableAutoScroll="True" />
                                                        <MasterTableView Width="100%" CommandItemDisplay="Top" DataKeyNames="MessageID,PhaseID"
                                                            ClientDataKeyNames="MessageID">
                                                            <SortExpressions>
                                                                <telerik:GridSortExpression FieldName="DateCreated" SortOrder="Descending" />
                                                                <telerik:GridSortExpression FieldName="DateCreated" SortOrder="Descending" />
                                                            </SortExpressions>
                                                            <Columns>
                                                                <telerik:GridTemplateColumn AllowFiltering="false" UniqueName="MessageID" DataField="MessageId">
                                                                    <HeaderStyle Width="20px" />
                                                                    <ItemTemplate>
                                                                        <asp:Image ID="imgFiles" runat="server" ImageUrl="~/Images/attachment-icon.png" Height="18px"
                                                                            Width="17px" />
                                                                    </ItemTemplate>
                                                                </telerik:GridTemplateColumn>
                                                                <telerik:GridBoundColumn SortExpression="Email" AutoPostBackOnFilter="true" HeaderText="From"
                                                                    HeaderButtonType="TextButton" DataField="Email" UniqueName="Email">
                                                                    <HeaderStyle Width="150px" />
                                                                </telerik:GridBoundColumn>
                                                                <telerik:GridBoundColumn SortExpression="MessageSubject" AutoPostBackOnFilter="true"
                                                                    HeaderText="Subject" HeaderButtonType="TextButton" DataField="MessageSubject"
                                                                    UniqueName="MessageSubject">
                                                                    <HeaderStyle Width="200px" />
                                                                </telerik:GridBoundColumn>
                                                                <telerik:GridBoundColumn DataFormatString="{0:MM/dd/yyyy 00:00:00}" SortExpression="DateCreated"
                                                                    AutoPostBackOnFilter="true"  AllowSorting=true DataType="System.TimeSpan" HeaderText="Date" HeaderButtonType="TextButton"
                                                                    DataField="DateCreated" UniqueName="DateCreated">
                                                                    <HeaderStyle Width="200px" />
                                                                </telerik:GridBoundColumn>
                                                                <telerik:GridBoundColumn SortExpression="SenderName" AutoPostBackOnFilter="true"
                                                                    HeaderText="Created By" HeaderButtonType="TextButton" DataField="SenderName"
                                                                    UniqueName="SenderName">
                                                                    <HeaderStyle Width="200px" />
                                                                </telerik:GridBoundColumn>
                                                                <telerik:GridTemplateColumn AllowFiltering="false" HeaderText="Phase Name" UniqueName="PhaseID"
                                                                    DataField="PhaseID">
                                                                    <HeaderStyle Width="200px" />
                                                                    <ItemTemplate>
                                                                        <asp:Label ID="lblPhase" runat="server" Text='<%#Eval("TagName").ToString() %>'></asp:Label>
                                                                    </ItemTemplate>
                                                                </telerik:GridTemplateColumn>
                                                            </Columns>
                                                        </MasterTableView>
                                                        <ClientSettings>
                                                            <ClientEvents OnRowContextMenu="RowContextMenu1"></ClientEvents>
                                                            <Selecting AllowRowSelect="true" />
                                                        </ClientSettings>
                                                        <ClientSettings AllowDragToGroup="True" AllowRowsDragDrop="true" ReorderColumnsOnClient="True"
                                                            AllowColumnHide="True" AllowRowHide="True" AllowColumnsReorder="True">
                                                            <Resizing EnableRealTimeResize="True" ResizeGridOnColumnResize="True" AllowColumnResize="True"
                                                                ClipCellContentOnResize="False"></Resizing>
                                                            <ClientEvents OnRowDblClick="ShowMessage" />
                                                            <Selecting AllowRowSelect="True" EnableDragToSelectRows="false" />
                                                            <Selecting AllowRowSelect="True" />
                                                            <ClientEvents OnRowDropping="onRowDropping" />
                                                        </ClientSettings>
                                                        <GroupingSettings CaseSensitive="false" />
                                                    </telerik:RadGrid>

Thanks.

8 Answers, 1 is accepted

Sort by
0
Tsvetina
Telerik team
answered on 20 Aug 2010, 03:12 PM
Hello Mandeep,

Could you specify what exactly do you mean by "not working" - does it throw an error or it does not sort properly?

Also, could you please share what are the results if you:
-Remove the initial sort and try to sort the column on header click? Does it sort normally this time?
-If not, what happens if you convert the TimeSpan data to string? I know that it will now be sorting alphabetically but does it sort without problems?

The answers to these questions will help us narrow down the set of possible reasons for the erroneous behavior.

All the best,
Tsvetina
the Telerik team
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
0
Shuaib
Top achievements
Rank 1
answered on 27 Dec 2011, 08:57 AM
Hi

I'm working on a similar issue.
I want to get my radgrid sorted on the 'Date' field in a descending order when the grid loads for the first time.
The grid is bound to a webmethod.
Can you guide me?

Thanks,
Shuaib
0
Shinu
Top achievements
Rank 2
answered on 27 Dec 2011, 10:06 AM
Hello,

Try the following code.
C#:
protected void Page_Load(object sender, EventArgs e)
   {
     GridSortExpression expression = new GridSortExpression();
     expression.FieldName = "BirthDate";
     expression.SortOrder = GridSortOrder.Descending;
     grid1.MasterTableView.SortExpressions.AddSortExpression(expression);
     grid1.MasterTableView.Rebind();
   }

-Shinu.
0
Tsvetina
Telerik team
answered on 27 Dec 2011, 03:54 PM
Hi Shuaib,

You can achieve this declaratively, for example:
<MasterTableView>
     <SortExpressions>
           <telerik:GridSortExpression FieldName="OrderDate" SortOrder="Descending" />
     </SortExpressions>
</MasterTableView>

You can also add it programmatically, similarly to what Shinu showed but without the Rebind() on Page_Load, as this is not recommended practice.

More information is available here.

Regards,
Tsvetina
the Telerik team
If you want to get updates on new releases, tips and tricks and sneak peeks at our product labs directly from the developers working on the RadControls for ASP.NET AJAX, subscribe to their blog feed now
0
Shuaib
Top achievements
Rank 1
answered on 28 Dec 2011, 04:12 PM
Hi Tsvetina

Thanks for your reply.
But I need to click the header for the grid to get sorted when I add a sort expression that way.
I want it to get sorted the first time it loads and shows up instead of clicking it to sort.

Thanks,
Shuaib
0
Tsvetina
Telerik team
answered on 29 Dec 2011, 10:21 AM
Hi Shuaib,

When using Shinu's code on initial Page_Load, the user does not need to do anything to get the sorting applied. You can see this in the following demo:
http://demos.telerik.com/aspnet-ajax/grid/examples/generalfeatures/sorting/defaultcs.aspx

Greetings,
Tsvetina
the Telerik team
If you want to get updates on new releases, tips and tricks and sneak peeks at our product labs directly from the developers working on the RadControls for ASP.NET AJAX, subscribe to their blog feed now
0
Waseem
Top achievements
Rank 1
answered on 17 Jan 2013, 07:38 PM
Hi

My RadGrid sorting does not work properly. I figure out that why its not working properly. I need its solution. Let me explain.

 public partial class TestPage: ParentClass  // System.Web.UI.Page
 {
 }

I inherit My ParentClass instead of System.Web.UI.Page. So RadGrid not working properly. I have some session mgt in ParentClass.
and My ParentClass is inherit from System.Web.UI.Page. But when i click on RadGrid sorting its does not properly. It sort only one time and some time i click on one column it sort other column.

Can you have any solution that why its happening?

If i did not inherit it from ParentClass and inherit it from System.Web.UI.Page . its work great.
Thanks,
Muhammad Waseem
the Telerik team
0
Mike
Top achievements
Rank 1
answered on 22 Jan 2013, 01:09 PM
Shinu - I know this is an old post but wanted to compliment you on your default sort code in Page_Load() - it works great!
Tags
Grid
Asked by
Mandy
Top achievements
Rank 2
Answers by
Tsvetina
Telerik team
Shuaib
Top achievements
Rank 1
Shinu
Top achievements
Rank 2
Waseem
Top achievements
Rank 1
Mike
Top achievements
Rank 1
Share this question
or