CGridview apply sorting on user-defined field


I am using CGridview for display my model’s data. There are 2 columns as “Subject1” and “Subject2” which display mark of those 2 subjects. In third column I want to display Total of those 2 columns. My code is for third column is :



  'value'=>'(($data->sub1 + $data->sub2) > 0) ? $data->sub1 + $data->sub2 : 0',


This code will display Total of Subject1 and Subject2. But "Total" column is not Sortable. Subject1 and Subject2 both columns are sortable.

How can I make "Total" column sortable?

Thanks in Advance!!

I did not check that, but I think similar to

you need to give it a name:




'value'=>'(($data->sub1 + $data->sub2) > 0) ? $data->sub1 + $data->sub2 : 0',


and provide sorting information for that name in your dataprovider:

new CActiveDataProvider( 'Post', array(





                'asc'=>'t.subtotal1 + t.subtotal2',

                'desc'=>'( t.subtotal1 + t.subtotal2 ) DESC',






Thank you!! It works fine for me.