Changing the Page Size Interactively in a DataGrid Web Control - The Implementation
(Page 4 of 5 )
For convenience we can wrap the work done in a function
Function NewCurrentPageIndex( _
dg As DataGrid, _
....ntRecordCount As Integer, _
....intNewPageSize As Integer _
) As Integer
The SelectedIndexChanged event of our page size setting DropDownList control has the procedure PageSizeChanged as its handler. This procedure looks like that:
Sub PageSizeChanged(sender As Object, e As EventArgs)
MyDataGrid.CurrentPageIndex = _
NewCurrentPageIndex(MyDataGrid, _
Session("recCount"), _
ddlPageSize.SelectedItem.Text)
BindGrid
End Sub
Here is the source of the NewCurrentPageIndex function:
Function NewCurrentPageIndex( _
dg As DataGrid, _
intRecordCount As Integer, _
intNewPageSize As Integer _
) As Integer
' old page size
Dim intOldPageSize As Integer
' top record index on current page
Dim intFirstRecordIndex As Integer
' new page count
Dim intNewPageCount As Integer
Dim intNewCurrentPageIndex as Integer
' is given from the DataGrid PageSize property
intOldPageSize = dg.PageSize
' identifies the reference record
intFirstRecordIndex = dg.CurrentPageIndex * intOldPageSize + 1
' set the new page size for the Data grig
dg.PageSize = intNewPageSize
' The actual page count of the DataGrid control
' is the "old" page count.
' The new page count of the DataGrid control will be set
' automatically after we bind the Datagrid to the data source
' with new page size set.
' We need the new page count already now
' to find out the new current page index,
' so we must calculate it.
intNewPageCount = _
CType(Math.Ceiling(intRecordCount / intNewPageSize),Integer)
' get the new current page index
Dim i as Integer
For i = 1 to intNewPageCount
If intFirstRecordIndex >= (i-1)*intNewPageSize +1 And _
intFirstRecordIndex <= i*intNewPageSize Then
intNewCurrentPageIndex=i-1
Exit For
End If
Next i
NewCurrentPageIndex = intNewCurrentPageIndex
End Function
Here (http://www.hatvany-online.net/aspdotnet/articles/changingpagesize.aspx) you can try a live demo. You can find the source code of the sample in the support file.
Next: Conclusion >>
More ASP.NET Articles
More By Csaba Hatvany