set initial sort on CGridView

I would like to set an initial sort for CGridView… that can be changed afterwards by clicking sortable columns header…

If I set an order in the criteria of the CActiveDataProvider the data in CGridView is sorted by that column constantly… that is clicking on other column headers (that are clickable) does not resort the data… even clicking on the sorted column does not change sorting from ASC to DESC… headers are clickable and the arrow near the column header text is displayed but data are not sorted on that column.

Example:

Table pcentar has two fields (id smallint, name vchar 20)

in the controler:




        $dataProvider=new CActiveDataProvider('Pcentar', array(

            'criteria'=>array( 'order'=>'id' ),

            'pagination'=>array( 'pageSize'=>self::PAGE_SIZE ),

        ));

        $this->render('index',array( 'dataProvider'=>$dataProvider));



in the view:




$this->widget('zii.widgets.grid.CGridView', array(

    'dataProvider'=>$dataProvider,

    'columns'=>array(

        'id',

        'name',

        array(

            'class'=>'CButtonColumn',

            'template'=>'{update} {delete}',

        ),

    ),

));



You can set ‘sort’=>array(‘defaultOrder’=>’…’)

I totally missed this one… thanks Qiang…

One thing about this default sorting

If I set for example:




        $dataProvider=new CActiveDataProvider('Pcentar', array(

            'sort'=>array(

                'defaultOrder'=>'id',

            ),

            'pagination'=>array(

                'pageSize'=>self::PAGE_SIZE,

            ),

        ));



when the data is displayed the ID column header does not have the small arrow indicating the sort and then happens something like this…

initially the data is displayed sorted by ID ascending because i set the sort as above but the column header does not indicate this.

If I click this column header then the header gets the arrow and the data remain sorted ascending

at the second click on the header the data gets sorted descending