What is the correct way to include the grid-view widget in form file

I have this code in my _form.php. The problem I am facing is if I am putting the pagination part below it does work, but filter doesn’t work. If I am putting the filter part below the pagination part, the filter part work and pagination doesn’t work.

Again my netbeans IDE is complaining that I have declared the $dataprovider twice.

here is my code.

Form part:


use yii\helpers\Html;

use yii\widgets\ActiveForm;

use yii\grid\GridView;

use yii\data\ActiveDataProvider;

use app\models\State;

use app\models\StateSearch;

use app\models\City;

/* @var $this yii\web\View */

/* @var $model app\models\State */

/* @var $form yii\widgets\ActiveForm */


<div class="row">

    <div class="col-lg-3">

<div class="col-lg-4 col-lg-offset-1">

<div class="state-form">

   <?php $form = ActiveForm::begin(); ?> 

    <?= $form->field($model, 'state_name')->textInput(['maxlength' => 50]) ?>

    <div class="form-group">

        <?= Html::submitButton($model->isNewRecord ? 'Create' : 'Update', ['class' => $model->isNewRecord ? 'btn btn-success' : 'btn btn-primary']) ?>


    <?php ActiveForm::end(); ?>


Pagination part


    $dataProvider = new ActiveDataProvider([

        'query' => State::find(),

        'pagination' => [

            'pageSize' => 5,




Filter part


   $searchModel = New StateSearch(); 

   $dataProvider = $searchModel->search(Yii::$app->request->queryParams);


Gridview Widget

<?= GridView::widget([

        'dataProvider' => $dataProvider,

        'filterModel' => $searchModel,

        'columns' => [

            ['class' => 'yii\grid\SerialColumn'],

           // 'id',


            ['class' => 'yii\grid\ActionColumn'],


    ]); ?>   




[*]What is the correct approach that my filter and pagination both works.

[*]How to show action column icons inline(that is horizontally). I can do it on view level, but need to know it can be done at global level.

[*]Can provide the pagination option on the grid-view for user selection like 5,10 or all.


Am I doing something grossly wrong?

Any pointer will be much appreciated.