CGridViewPagination not working with custom query

I have a form in my view which selects columns from a dropdown list. The SqlDataProvider is used to get the data from the generated Query

I have been trying to use CGridView with SQLDataProvider, its working a bit fine, but still having some issues with pagination. I don’t have any keyField so I give it my first column, and without keyField it doesn’t work.

Here is my action:

public function actionIndex()


    $tables = Yii::app()->db->schema->getTables();

    if(isset($_POST['yt0'])){ //if from submitted

        $query = $this->generateQuery();

        $count=Yii::app()->db->createCommand('SELECT COUNT(*) FROM ( ' . $query . ' ) as count')->queryScalar();

        $dataProvider = new CSqlDataProvider($query, array(

            'keyField' => $firstColumn,

            'totalItemCount'=> $count,

            'pagination'=> array(




        $columns = $this->getColumnNamesWithoutTable();


            array(  'tables' => $tables,

                    'query' => $query,

                    'dataProvider' => $dataProvider,

                    'columns' => $columns




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



Here is widget code in the view, according to what I have read, pagination should work automatically, is it because I am using a custom query rather than Yii Models ?

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


            'enablePagination'=> true,


It shows the data first time, but next button doesn’t work.

Why don’t you try writing a j script ?

I had similar problem while working in a project. Coz its not getting parameter for pages. Can you brief how are you sending "yt0" and page parameter together?

Try something like this.

Yii::app()->clientScript->registerScript('js_listview', <<< EOT_JS_LISTVIEW

    var yt0Val = $('#yt0').val();

 $('div.keys').each(function(n) {

  var title = $(this).attr('title');

  title = title + '/yt0/' + yt0Val;

  $(this).attr('title', title);




I hope it till some extinct helped you… Cheers…