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:




<?php


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']) ?>

    </div>


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


</div>



Pagination part




<?php

    $dataProvider = new ActiveDataProvider([

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

        'pagination' => [

            'pageSize' => 5,

        ],

    ]);

?>

Filter part


<?php  

   $searchModel = New StateSearch(); 

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

   ?>

Gridview Widget


<?= GridView::widget([

        'dataProvider' => $dataProvider,

        'filterModel' => $searchModel,


        'columns' => [

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

           // 'id',

            'state_name',

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

        ],

    ]); ?>   

</div>

</div>

[list=1]

[*]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.

[/list]

Am I doing something grossly wrong?

Any pointer will be much appreciated.

Thanks.