Working with DataGrids - How to Add Paging to the DataGrid
(Page 3 of 5 )
To make our above DataGrid display paging, we have to adjust simple properties -- simply add attributes [ AllowPaging="true" onPageIndexChanged="NavigatePage" ] at Declaration so that after these properties, your DataGrid declaration looks like:
<Asp:DataGrid Id="MyGrid" runat="Server" AllowPaging="true" onPageIndexChanged="NavigatePage" />
Here NavigatePage is the Method which responds when a user clicks on the page number. I will explain it in a moment. Before that in page load, we have to adjust DataGrid properties like this:
Public Sub Page_Load()
If (not isPostBack) then
MyGrid.PageSize=10
MyGrid.PagerStyle.Mode=PagerMode.NumericPages
MyGrid.CurrentPageIndex=0
LoadData()
end if
End Sub
Assign the page size (I:e How many rows per page), page style and page index.
Page indexing will be in numerical mode for most of the applications, but the time comes when page indexing also exceeds more than one row; it won't look nice in that case. To overcome this we have "PagerStyle" Property.
Here page mode implies whether you want numerical page index or just "Next" "Previous" style. PagerStyle.Mode=PagerMode.NumericPages gives the numerical index at the bottom of the Grid.
PagerStyle.Mode= PagerMode.NextPrev provides "Next" and "Previous" hyperlinks at the bottom of the Grid. By default, the indexing will be aligned to left; to make right aligned, just add the attribute PagerStyle-HorizontalAlign=”Right” at Declaration so that the page indexing will be right aligned.
In case of PrevNext mode we can change the text also with properties like:
MyGrid,PagerStyle.NextPageText= ”MoveNext”
MyGrid.PagerStyle.PrevPageText=” MoveBack”
Next: Navigate Page >>
More ASP.NET Articles
More By JB Reddy