Zo Has
Zo Has

Reputation: 13018

Implementing pager control for gridview?

Is there a way to select the number of records/rows to display in the gridview by a drop down list ?

Upvotes: 0

Views: 1184

Answers (2)

Tim Schmelter
Tim Schmelter

Reputation: 460068

You can also use RowCreated to create your Dropdownlist in Codebehind. Have a look at following example(VB.Net):

    Private Sub Yourgrid_RowCreated(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles Yourgrid.RowCreated
        Select Case e.Row.RowType
            Case DataControlRowType.Pager
                Dim ddlPager As New DropDownList
                ddlPager.ID = "DdlPager"
                ddlPager.AutoPostBack = True
                ddlPager.ToolTip = "Change Pagesize"
                ddlPager.Items.Add("5")
                ddlPager.Items.Add("10")
                ddlPager.Items.Add("25")
                ddlPager.Items.Add("50")
                ddlPager.Items.Add("100")
                ddlPager.SelectedValue = "10"
                AddHandler ddlPager.SelectedIndexChanged, AddressOf Me.PageSizeChanged
                e.Row.Cells(0).ColumnSpan -= 1
                Dim td As New TableCell
                Dim span1 As New Label
                span1.Text = "Show"
                span1.Style("margin-left") = "50px"
                td.Controls.Add(span1)
                td.Controls.Add(ddlPager)
                Dim span2 As New Label
                span2.Text = "rows per page"
                td.Controls.Add(span2)
                e.Row.Cells.Add(td)
        End Select
    End Sub

    Private Sub PageSizeChanged(ByVal sender As Object, ByVal e As System.EventArgs)
        Dim ddlPager As DropDownList = DirectCast(sender, DropDownList)
        Dim newPageSize As Int32 = Int32.Parse(ddlPager.SelectedValue)
        YourGrid.PageSize = newPageSize 'change the PageSize of the Grid'
        DataBindYourGrid() 'call the function that Binds your grid to the Datasource'

        UpdYourgrid.Update() 'if you use Ajax, update the UpdatePanel of this GridView'
    End Sub

On this way you autogenerate the Dropdonwlist on every postback and add it to the Gridview's pager. The code is reusable for any GridView.

Upvotes: 0

Mark Kadlec
Mark Kadlec

Reputation: 8440

If you mean a dynamic change of the number of rows based on a DDL selection, sure it can be done.

I would suggest using an AJAX method on the select action that would query the DB for the exact amount of rows and returning. Far too often I've seen a query bring back thousands of rows and the paging etc is done in memory. Much more efficient to just get the rows/page directly from the DB and preserve bandwidth.

Not sure if that is exactly what you were asking, but hope it helps.

Upvotes: 1

Related Questions