Monday, July 30, 2007

ASP:GridView - making Pager always visible

You would have noticed this while implementing ASP.net GridView control that if the record count in the grid is lesser than the grid's page size, then the pager doesn't appear.

There can be times when you want the pager to be visible all the time (like when you implement custom paging). In such a case there is no property available in the grid which can directly let you do so. But, there is a workaround to make this happen.

To make this happen, you will have to do following -

1. Handle the GridView's PreRender event.
2. In the PreRender event, fetch the GridView's 'BottomPagerRow'
3. Set BottomPagerRow's 'Visible' property to 'True'.

Example :

Private Sub gvItems_PreRender(ByVal sender As Object, ByVal e As System.EventArgs)Handles gvItems.PreRender

Dim gv As System.Web.UI.WebControls.GridView = CType(sender, System.Web.UI.WebControls.GridView)

    If Not gv Is Nothing Then

        Dim PagerRow As GridViewRow = gv.BottomPagerRow

        If Not PagerRow Is Nothing Then

            PagerRow.Visible = True

        End If

    End If

End Sub



Thats it!