paging on gridviews

Jay1b

Member
Joined
Dec 12, 2007
Messages
21
Programming Experience
5-10
Hi

I've ran into some troubles with paging on gridviews. This is a cut down version of my code.

VB.NET:
Dim dt as new datatable

'Build up columns and rows in dt using vast quantities of code (pivot table)

'gv is the name of my gridview
gv.datasource = dt
gv.databind()

The above works fine except for the paging element. I'm guessing i need to run it via a dataview or a objectdatasource. I've searched the internet for examples of them working, but I cant find any working examples which arent vastly to overly complicated for what I need.

Could someone help me pls?

Thanks
 
You can use the built in paging in GridView with your current source, just set AllowPaging and add the PageIndexChanging event handler where you add this code:
VB.NET:
gv.PageIndex = e.NewPageIndex
gv.DataSource = getdata()
gv.DataBind()
getdata is the function that return the data source (the full table), in your case here you create the datatable and return it.

A ObjectDataSource can also be used, with this you can get better performance by only returning current page of data for each postback. Here is a sample basic setup with ObjectDataSource, if you want to try; add this class to App_Code folder:
VB.NET:
Public Class datas
    Function getdata(ByVal startRowIndex As Integer, ByVal maximumRows As Integer) As Data.DataTable
        Dim t As New Data.DataTable
        Dim r As Data.DataRow
        t.Columns.Add("col1")
        For i As Integer = startRowIndex To startRowIndex + maximumRows
            r = t.NewRow
            r(0) = i.ToString
            t.Rows.Add(r)
        Next
        Return t
    End Function
    Function getcount() As Integer
        Return 100 'an arbitrary number, should return the total number of rows of data source
    End Function
End Class
Configure GridView DataSourceID property in designer view where you select the obj.datasource object.
Configure the ObjectDataSource properties in designer view:
SelectMethod: getdata
TypeName: datas
EnablePaging: True
SelectCountMethod: getcount
Notice how the MaximumRowsParameterName and StartRowIndexParameterName is reflected with the parameter names of the getdata function above.
 
Back
Top