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

How to sort the grid in descending order on page load itself

1 Answer 131 Views
Grid
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
Lavanya
Top achievements
Rank 1
Lavanya asked on 04 May 2011, 02:33 PM
Hi there,

I am using a teleric grid for my MVC application.
I am displaying the data in grid too..
I need to display the details in descending order i.e., the most recent rows in the content history table has to be displayed.

I am using the code as follows:( The bold ones)

 [GridAction]
        public ActionResult _GetHistory(int? ID)
        {
            if (ID.HasValue)
            {
                var id = from c in _DataContext.ApplicationLabels
                         where c.ID == ID
                         select c;
                ApplicationLabel applable = id.First();
                var history =  from h in _DataContext.ContentHistories
                              where h.Modified <= DateTime.Now
                              orderby h.Modified descending
                              select new HistoryViewModel

                             {

                                 Name = h.Terminology.Name,
                                 Data = h.Data,
                                 Modified = h.Modified,
                                 UserName = h.aspnet_Users.UserName
                             };
                return View(new GridModel
                {
                    Data = history.OrderByDescending(o => o.Modified)
                });
            }
            else
            {
                var history = from h in _DataContext.ContentHistories
                              where h.Modified <= DateTime.Now
                              select new HistoryViewModel
                             {
                                 Name = h.Terminology.Name,
                                 Data = h.Data,
                                 Modified = h.Modified,
                                 UserName = h.aspnet_Users.UserName
                             };
                return View(new GridModel
                {
                    Data = history.OrderByDescending(o=>o.Modified).ToList()
                });

view.aspx:
 <p>
            Orders:
            <%= Html.Telerik().Grid<ResourceKit.Models.HistoryViewModel>()
                    .Name("ContentHistory")       
                    .DataBinding(binding => binding.Ajax().Select("_GetHistory", "Application"))
                    .Columns(column =>
                    {
                        column.Bound(ch => ch.Name);
                        column.Bound(ch => ch.Data).Title("Data");
                        column.Bound(ch => ch.Modified).Title("Modified");
                        column.Bound(ch => ch.UserName).Title("UserName");

                    }).Pageable().Sortable()                              
            %>
    </p>


Please let me know what changes has to be made inorder to display the details in descending order by default.

Thanks,
Lavanya

1 Answer, 1 is accepted

Sort by
0
Lavanya
Top achievements
Rank 1
answered on 05 May 2011, 07:20 AM
Hi,

I finally got it done!!;)
The answer is:

Initially my code was:

Data = history.OrderByDescending(o=>o.Modified)

I changed it to:

Data = history.OrderByDescending(o=>o.Modified).ToList()

Regards,
Lavanya
Tags
Grid
Asked by
Lavanya
Top achievements
Rank 1
Answers by
Lavanya
Top achievements
Rank 1
Share this question
or