Datagrid Paging ... Make it look like google..

cyclonebri

Regular
Joined
Jul 30, 2003
Messages
93
Location
Ames, IA, USA
Hi everybody,

I have a question that I have not seen much on, so I thought I would ask it here and see if anyone else knows how to do this or if it is even possible.

My boss would like to see our datagrids have a multiple style that I dont' know if I can do. He would like to see something that looks almost identical to the paging on a regular google search...in other words:

<Previous 1 2 3 4 5 6 7 8 9 ... Next>

Right now I can easily implement the
<Previous Next>
or the
1 2 3 4 5 6 7 8 9 ...

but I would like to combine them both into the paging that is at the bottom of the grid. Has anyone ever done this? Is it possible? Any resources or hints would be greatly appreciated! Thanks!
 
Yeah it's too bad that many of these article including many books don't use code-behind. I'm sure you'll figure it out.
 
I hate to dig up an old post, but I believe in sharing knowledge with those who have always helped me. I have learned how to create a very awesome datagrid pager that looks like this
<< First << Prev 1 2 3 4 Next >> Last >>

and in addition it only shows the first and previous when they are really available as well as the next and last. I know that others will be interested in doing this so I felt it would be great to post it here in case others search and find as hard of a time implementing the code from the four guys from rolla as I had. I saw some of this in a magazine and then modified it to do what I needed. Please notice that you need to add the calling code in the datagrid_ItemCreated(..) event:

Visual Basic:
Private Sub dg1_ItemCreated(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.DataGridItemEventArgs) Handles dg1.ItemCreated
        Try
            If e.Item.ItemType = ListItemType.Pager Then
                Dim pager As TableCell = e.Item.Cells(0)
                ' Next>> Last>>
                AddLinkNextPage(pager)
                AddLinkLastPage(pager)

                '<<First <<Prev
                AddLinkPrevPage(pager)
                AddLinkFirstPage(pager)
            End If
        Catch ex As Exception
            System.Diagnostics.Trace.WriteLine(ex.ToString)
        End Try
    End Sub

    Private Sub AddLinkFirstPage(ByVal pager As TableCell)
        Try
            If TypeOf (pager.Controls(0)) Is LinkButton Then
                Dim btnFirst As LinkButton = New LinkButton
                btnFirst.Font.Name = "Tahoma"
                btnFirst.Font.Size = pager.Font.Size.Smaller
                btnFirst.ForeColor = dg1.PagerStyle.ForeColor
                btnFirst.Text = "<< First "
                AddHandler btnFirst.Click, AddressOf GoToFirstPage
                pager.Controls.AddAt(0, btnFirst)
            Else
                Dim btnFirst As Label = New Label
                btnFirst.Font.Name = "Tahoma"
                btnFirst.Font.Size = pager.Font.Size.Smaller
                btnFirst.ForeColor = dg1.PagerStyle.ForeColor
                btnFirst.Text = ""
                pager.Controls.AddAt(0, btnFirst)
            End If
        Catch ex As Exception
            System.Diagnostics.Trace.WriteLine(ex.ToString)
        End Try
    End Sub

    Private Sub AddLinkPrevPage(ByVal pager As TableCell)
        Try
            If TypeOf (pager.Controls(0)) Is LinkButton Then
                Dim btnPrev As LinkButton = New LinkButton
                btnPrev.Font.Name = "Tahoma"
                btnPrev.Font.Size = pager.Font.Size.Smaller
                btnPrev.ForeColor = dg1.PagerStyle.ForeColor
                btnPrev.Text = "<< Prev "
                AddHandler btnPrev.Click, AddressOf GoToPrevPage
                pager.Controls.AddAt(0, btnPrev)
            Else
                Dim btnPrev As Label = New Label
                btnPrev.Font.Name = "Tahoma"
                btnPrev.Font.Size = pager.Font.Size.Smaller
                btnPrev.ForeColor = dg1.PagerStyle.ForeColor
                btnPrev.Text = ""
                pager.Controls.AddAt(0, btnPrev)
            End If

        Catch ex As Exception
            System.Diagnostics.Trace.WriteLine(ex.ToString)
        End Try
    End Sub

    Private Sub AddLinkNextPage(ByVal pager As TableCell)
        Try
            Dim ccount As Integer = pager.Controls.Count
            If TypeOf (pager.Controls(pager.Controls.Count - 1)) Is LinkButton Then
                Dim btnNext As LinkButton = New LinkButton
                btnNext.Font.Name = "Tahoma"
                btnNext.Font.Size = pager.Font.Size.Smaller
                btnNext.ForeColor = dg1.PagerStyle.ForeColor
                btnNext.Text = " Next >>"
                AddHandler btnNext.Click, AddressOf GoToNextPage
                pager.Controls.AddAt(ccount, btnNext)
            Else
                Dim btnNext As Label = New Label
                btnNext.Font.Name = "Tahoma"
                btnNext.Font.Size = pager.Font.Size.Smaller
                btnNext.ForeColor = dg1.PagerStyle.ForeColor
                btnNext.Text = ""
                pager.Controls.AddAt(ccount, btnNext)
            End If
        Catch ex As Exception
            System.Diagnostics.Trace.WriteLine(ex.ToString)
        End Try
    End Sub

    Private Sub AddLinkLastPage(ByVal pager As TableCell)
        Try
            Dim ccount As Integer = pager.Controls.Count
            If TypeOf (pager.Controls(pager.Controls.Count - 1)) Is LinkButton Then
                Dim btnLast As LinkButton = New LinkButton
                btnLast.Font.Name = "Tahoma"
                btnLast.Font.Size = pager.Font.Size.Smaller
                btnLast.ForeColor = dg1.PagerStyle.ForeColor
                btnLast.Text = " Last >>"
                AddHandler btnLast.Click, AddressOf GoToLastPage
                pager.Controls.AddAt(ccount, btnLast)
            Else
                Dim btnLast As Label = New Label
                btnLast.Font.Name = "Tahoma"
                btnLast.Font.Size = pager.Font.Size.Smaller
                btnLast.ForeColor = dg1.PagerStyle.ForeColor
                btnLast.Text = ""
                pager.Controls.AddAt(ccount, btnLast)
            End If
        Catch ex As Exception
            System.Diagnostics.Trace.WriteLine(ex.ToString)
        End Try
    End Sub

    Private Sub GoToFirstPage(ByVal sender As Object, ByVal e As EventArgs)
        dg1.CurrentPageIndex = 0
        dg1.DataBind()
    End Sub

    Private Sub GoToPrevPage(ByVal sender As Object, ByVal e As EventArgs)
        If dg1.CurrentPageIndex - 1 > 0 Then
            dg1.CurrentPageIndex -= 1
        Else
            dg1.CurrentPageIndex = 0
        End If
        dg1.DataBind()
    End Sub

    Private Sub GoToNextPage(ByVal sender As Object, ByVal e As EventArgs)
        If dg1.CurrentPageIndex + 1 < dg1.PageCount - 1 Then
            dg1.CurrentPageIndex += 1
        Else
            dg1.CurrentPageIndex = dg1.PageCount - 1
        End If
        dg1.DataBind()
    End Sub

    Private Sub GoToLastPage(ByVal sender As Object, ByVal e As EventArgs)
        dg1.CurrentPageIndex = dg1.PageCount - 1
        dg1.DataBind()
    End Sub

Enjoy,
Brian
 
Back
Top