Datasheet

PageSize, which defines the number of rows to show per page.
OnPageIndexChanged, which defines the event procedure to call when the page number is
changed. When a page number link is clicked, the procedure defined here is run.
AllowSorting, which is true, allowing the grid to sort the rows on the basis of column
selections. Setting this to
true enables links on the column headings.
OnSortCommand, which defines the event procedure to call when a column heading is clicked.
Now let's look at the code that uses this grid, starting with the
Page_Load event:
Sub Page_Load(Sender As Object, E As EventArgs)
If Not Page.IsPostBack Then
BindGrid()
End If
End Sub
Here we are calling the BindGrid routine, but only if this is the first time the page has been loaded. This
ensures that the grid, in its initial state, displays data in a default sort order. You'll see how this works as
we go through the code.
Next we have two events for the grid. The first is for when a page is selected on the grid, and is the event
procedure defined in the
OnPageIndexChanged attribute:
Sub DataGrid_Page(Sender As Object, e As DataGridPageChangedEventArgs)
DataGrid1.CurrentPageIndex = e.NewPageIndex
BindGrid()
End Sub
Notice that the second argument to this procedure is of type DataGridPageChangedEventArgs. This is
automatically sent by ASP.NET and contains two properties, only one of which we are interested
in
NewPageIndex. This identifies the number of the page selected, so we set the CurrentPageIndex
property of the grid to the selected page number. We then call the BindGrid routine to re-fetch the data
and bind it to the grid. Later, we'll look at why you need to do this.
The second event procedure is for sorting the grid, and is defined in the
OnSortCommand attribute:
Sub DataGrid_Sort(Sender As Object, e As DataGridSortCommandEventArgs)
DataGrid1.CurrentPageIndex = 0
SortField = e.SortExpression
BindGrid()
End Sub
The second argument for this procedure is of type DataGridSortCommandEventArgs, which contains
the expression on which the grid is being sorted. In this case, this is automatically set by the
DataGrid
as the column headings are sortable, and so contains the column name.
262
Chapter 8
57076_Ch 8 SAN.qxd 01/12/2003 6:43 PM Page 262