It worked for me doing a little changes of the following methods, i am not sure tho if its how the author of the post meant to make this work or its completely correct according to some programmatical standars.
Imports System.Data
Imports System.Data.SqlClient
Public Class MyTableAdapter
Dim dt As DataTable
Dim vic As Integer 'virtual item count
Public Sub New(ByVal dtp As DataTable, ByVal vicp As Integer)
dt = dtp
vic = vicp
End Sub
'returns the filled datatable
Public Function GetData() As DataTable
Return dt
End Function
'returns the number of records
Public Function VirtualItemCount() As Integer
Return vic
End Function
'this also returns the datatable but the ODS needs it for paging purposes
Public Function GetData(ByVal startRow As Integer, ByVal maxRows As Integer) As DataTable
Return dt
End Function
End Class
----
Imports System.Data
Imports System.Data.SqlClient
Public Class MyGridViewFiller
Private dt As New DataTable
Private vic As Integer
Private callerpage As Page
Private gid As String
Public Sub CreateCuteLittleTable(ByVal pagep As Page, ByVal gidp As String, _
ByVal regini As Integer, ByVal numregporpag As Integer)
'This is the stored procedure which retrieves the records based on the following parameters
Dim dap As New SqlDataAdapter("damenordenesdea", bd.globalcn)
dap.SelectCommand.CommandType = CommandType.StoredProcedure
'Initial Record from which the rest of records will be displayed
dap.SelectCommand.Parameters.Add("@nregini", SqlDbType.Int).Value = regini
'Number of Records per page
dap.SelectCommand.Parameters.Add("@nregporpag", SqlDbType.Int).Value = numregporpag
'Retrieve the number of total records spit out by the query
dap.SelectCommand.Parameters.Add("@nregis", SqlDbType.Int).Direction = ParameterDirection.Output
dap.Fill(dt)
'Set the vic variable with the total amount of records to display
vic = dap.SelectCommand.Parameters("@nregis").Value
'lets store the page we are in and the gridviewid of that page
callerpage = pagep
gid = gidp
FillGridView()
End Sub
Protected Sub ods_ObjectCreating(ByVal sender As Object, ByVal e As ObjectDataSourceEventArgs)
e.ObjectInstance = New MyTableAdapter(dt, vic)
End Sub
Public Sub FillGridView()
'First let's make a GridView object that hold the GridView
Dim gv As GridView = CType(callerpage.FindControl(gid), GridView)
'Then create an ObjectDateSource object programmatically
Dim ods As New ObjectDataSource
'setting the necessary properties (these will interact with our TableAdapter !)
ods.ID = "ods" & gid
'the paging of ODS depends on the paging of the GridView
ods.EnablePaging = gv.AllowPaging
ods.TypeName = "MyTableAdapter"
ods.SelectMethod = "GetData"
ods.SelectCountMethod = "VirtualItemCount"
ods.StartRowIndexParameterName = "startRow"
ods.MaximumRowsParameterName = "maxRows"
ods.EnableViewState = True
'Instead of calling the parameterless constructor of our TableAdapter, I'm going
'to hook up on the ods.ObjectCreating event to supply my own TableAdapter (WITH constructor parameters)
'the following line is the "hook up" declaration
'this means that at the object creating event, the CLR is going to jump to my event handler (ods_ObjectCreating)
AddHandler ods.ObjectCreating, New ObjectDataSourceObjectEventHandler(AddressOf ods_ObjectCreating)
'after that, we only need to bind the ODS to the GridView
gv.DataSource = ods
gv.DataBind()
End Sub
End Class
---
The Default.aspx page which will alocate the gridview
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If Not Page.IsPostback Then
Dim gvf As New MyGridViewFiller
gvf.CreateCuteLittleTable(Me.Page, Me.GridView1.ID, 0, 10)
End If
End Sub
Protected Sub GridView1_PageIndexChanging(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewPageEventArgs) Handles GridView1.PageIndexChanging
Me.GridView1.PageIndex = e.NewPageIndex
Dim gvf As New MyGridViewFiller
'the method pass the page, the gridviewid, the inital record number to be displayed according to the actual gridview pageindex, and the amount of records per page
gvf.CreateCuteLittleTable(Me.Page, Me.GridView1.ID, ((Me.GridView1.PageIndex + 1) * Me.GridView1.PageSize) -
(Me.GridView1.PageSize), Me.GridView1.PageSize)
End Sub
End Class
---
This is the storedprocedure which handles the paging, the example works with the table orders from the Northwind Database.
create procedure damenordenesdea
@nregini int,---number of the starting record to be displayed
@nregporpag int,---number of records to be displayed per gridview page
@nregis int output ---total number of the records resulted from the query
as
Declare @msql nvarchar(500)
set @msql=N'SELECT @nregis=count(*) from orders SELECT top ' + convert(varchar,@nregporpag) + ' * FROM orders
WHERE orderid
NOT IN
(SELECT TOP ' + convert(varchar,@nregini) + ' orderid FROM orders order by orderid asc)
order by orderid asc'
exec sp_executesql @msql,N'@nregini int,@nregporpag int,@nregis int output',@nregini=@nregini,@nregporpag=@nregporpag,@nregis=@nregis output
I have a working example if you want to test it, just let me know and i will send it to you.
I hope it helped you.
Choro.
Be like water making its way through cracks, be formless, shapeless....
-Bruce Lee-