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

Sort by column text instead of datasource field

2 Answers 622 Views
Grid
This is a migrated thread and some comments may be shown as answers.
John
Top achievements
Rank 1
John asked on 22 Jun 2011, 02:43 AM
Hello,

We have a RadGrid which is displaying some jobs.  It has 3 columns:  Job name, Run Time and Status.  In the ItemDataBound event of the RadGrid, we have a case statement which changes the text value of Run Time depending on the status of the job.  If the job is scheduled to run in the future, its next run date is shown in the Run Time column.  However, if the job is either running or pending, the job's last modified date is shown in the Run Time column. 

The problem that we have now is that we would like to sort by the value in the Run Time column, but since the Run Time column does not have one underlying datasource field, the sorting is not working correctly.  Is there a way to sort by the text value in the column and not by an underlying datasource field? 

If anyone also has an alternate idea, I'd welcome that as well; it's always possible that I'm overlooking a better solution.  :)  Thanks in advance for any help that anyone can give.

Regards,
John

2 Answers, 1 is accepted

Sort by
0
Accepted
Veli
Telerik team
answered on 24 Jun 2011, 09:12 AM
Hi John,

You cannot use RadGrid's built-in sorting to sort by arbitrary text in table cells. Sorting is always applied to data values as retrieved at the time of databinding. If you want to implement this scenario, you can take 1 of 2 options:

1. Find a way to include your calculated values as a data column in RadGrid's data source. Thus you get automatic filtering once your values appear in the grid data.

2. Use custom sorting and implement sorting on your text fields programmatically. This is done by using a couple of events in RadGrid.

Generally, RadGrid allows custom sorting by setting RadGrid.MasterTableView.AllowCustomSorting = true. Using this option, RadGrid will manage your sort state and SortExpressions, but will not sort the actual data. The implementer is responsible for writing the sorting logic and returning sorted data in the NeedDataSource event handler.

In your case, we want to implement a mixed mode sorting. You want default sorting for all fields but one. For this to work, we should not enable custom sorting permanently. Instead, custom sorting is enabled only when we detect sorting is to occur for the field containing custom values. You need to use 3 events to setup this scenario. The examples below assume your custom sorting field is CategoryID:

1. ItemCommand event is used to detect Sort command and check the data field. If CategodyID is to be sorted, custom sorting is enabled:

protected void RadGrid1_ItemCommand(object sender, GridCommandEventArgs e)
{
    if (e.CommandName == RadGrid.SortCommandName)
    {
        var sortCommandArgs = (GridSortCommandEventArgs)e;
        if (sortCommandArgs.SortExpression == "CategoryID")
        {
            RadGrid1.MasterTableView.AllowCustomSorting = true;
        }
    }
}

2. Use NeedDataSource to specify RadGrid.DataSource. Check the sort expression in the master table and implement custom sorting for the CategoryID field if any of the sort expressions contains this field name:

void RadGrid1_NeedDataSource(object source, GridNeedDataSourceEventArgs e)
{
    if (RadGrid1.MasterTableView.SortExpressions.Count > 0)
    {
        foreach (GridSortExpression expr in RadGrid1.MasterTableView.SortExpressions)
        {
            if (expr.FieldName == "CategoryID")
            {
                //sort by CategoryID here (as required by your business logic) and set the sorted data to RadGrid.DataSource
                RadGrid1.DataSource = MyData.BusinessDataStorage.GetData().OrderByDescending(item => item.CategoryID);
                return;
            }
        }
    }
 
    RadGrid1.DataSource = MyData.BusinessDataStorage.GetData();
}


3. DataBound is used to disable custom sorting to have built-in sorting applied for other columns:

protected void RadGrid1_DataBound(object sender, EventArgs e)
{
    RadGrid1.MasterTableView.AllowCustomSorting = false;
}

With the above setup, we get built-in sorting for all grid columns except for the CategoryID column which we sort programmatically.

Veli
the Telerik team

Browse the vast support resources we have to jump start your development with RadControls for ASP.NET AJAX. See how to integrate our AJAX controls seamlessly in SharePoint 2007/2010 visiting our common SharePoint portal.

0
John
Top achievements
Rank 1
answered on 27 Jun 2011, 10:04 PM
Hi Veli,

Thank you so much for your response.  Your response gave me an idea and I went with a mixture of what you suggested. 

In the "OnNeedDataSource" event, I used the following method:

protected void jobQueueRadGrid_NeedDataSource(object source, GridNeedDataSourceEventArgs e)
        {
            var adjustedJobQueueList = from job in new Jobs().GetJobQueueJobs()
                            select new
                            {
                                JobID = job.ID,
                                LstnrName = job.ScheduleListener.ComputerName,
                                LstnrID = job.ScheduleListener.ID,
                                 job.JobName,
                                 job.JobStatusString,
                                 SubmitTime = job.JobStatus == JobStatus.Ready ? job.NextRunString :
                                                         job.LastModifiedDate.ToString()
                             };
 
            this.jobQueueRadGrid.DataSource = adjustedJobQueueList;
        }

I just used a Linq query to basically add the column and then I set the data source.  After that, the sorting works out just fine.  Thanks for your help and the suggestions which led to the final solution; it was a huge help!

Regards,
John
Tags
Grid
Asked by
John
Top achievements
Rank 1
Answers by
Veli
Telerik team
John
Top achievements
Rank 1
Share this question
or