that item into our local variable, dataSet. Regardless of how the DataSet is
loaded, we bind it to our GridView as we did before.
If you save your work and load the Departments page in your browser, you should
see that the page runs exactly as it did previously, except that now the database
is accessed only once, the first time the page loads.
Implementing Sorting
To implement sorting functionality, we need to understand a few details of the
inner workings of data binding.
Technically, you cant bind a DataSet to a GridView control, because a DataSet
can contain many tables, whereas the GridView control can only handle one set
of rows and columns. However, by virtue of the fact that your DataSet has only
contained a single DataTable, the GridView control has been smart enough to
figure out that what you probably meant was the following:
Visual Basic
' Bind the grid to the DataSet
departmentsGrid.DataSource = dataSet.Tables("Departments")
departmentsGrid.DataBind()
C#
// Bind the grid to the DataSet
departmentsGrid.DataSource = dataSet.Tables["Departments"];
departmentsGrid.DataBind();
However, this isnt technically correct in the strictest sense, either. All of the
GridViews data binding is actually done through DataView objects. Thankfully,
each DataTable has a DefaultView property, which the GridView will automat-
ically use whenever you bind it to a DataTable. So, the following code listings
have the same functionality as those we saw above:
Visual Basic
' Bind the grid to the DataView
departmentsGrid.DataSource = _
dataSet.Tables("Departments").DefaultView
departmentsGrid.DataBind()
C#
// Bind the grid to the DataView
departmentsGrid.DataSource =
509
Implementing Sorting
dataSet.Tables["Departments"].DefaultView;
departmentsGrid.DataBind();
DefaultView does not Apply when Binding to a DataSet
Its interesting to note that if you bind directly to a DataSet that contains
only one table, that tables DefaultView will not be used; the GridView
will generate a separate DataView itself.
DataViews represent a customized view of a DataSet for sorting, filtering,
searching, editing, and navigation. When binding a GridView directly to a
DataTable, the DefaultView property, which is a DataView object, is accessed
automatically for us. However, if we want to enable sorting, we need to access
the DataView and set its sorting parameters.
The first step to enabling sorting is to set the AllowSorting property to True.
When we do that, the grids column headings become hyperlinks. Before we make
those hyperlinks work, we need to handle the grids Sorting event, where we
teach the grid what to do when those links are clicked.
Set the AllowSorting property of the GridView control in Departments.aspx
to True, then use the designer to generate the handler for the GridViews Sorting
event. Then, complete the code as shown:
Visual Basic File: Departments.aspx.vb (excerpt)
Protected Sub departmentsGrid_Sorting(ByVal sender As Object, _
ByVal e As System.Web.UI.WebControls.GridViewSortEventArgs) _
Handles departmentsGrid.Sorting
' Retrieve the name of the clicked column (sort expression)
Dim sortExpression As String = e.SortExpression
' Decide and save the new sort direction
If (sortExpression = gridSortExpression) Then
If gridSortDirection = SortDirection.Ascending Then
gridSortDirection = SortDirection.Descending
Else
gridSortDirection = SortDirection.Ascending
End If
Else
gridSortDirection = WebControls.SortDirection.Ascending
End If
' Save the new sort expression
gridSortExpression = sortExpression
' Rebind the grid to its data source
BindGrid()
End Sub
510
Chapter 12: Advanced Data Access
Private Property gridSortDirection()
Get
' Initial state is Ascending
If (ViewState("GridSortDirection") Is Nothing) Then
ViewState("GridSortDirection") = SortDirection.Ascending
End If
' Return the state
Return ViewState("GridSortDirection")
End Get
Set(ByVal value)
ViewState("GridSortDirection") = value
End Set
End Property
Private Property gridSortExpression()
Get
' Initial sort expression is DepartmentID
If (ViewState("GridSortExpression") Is Nothing) Then
ViewState("GridSortExpression") = "DepartmentID"
End If
' Return the sort expression
Return ViewState("GridSortExpression")
End Get
Set(ByVal value)
ViewState("GridSortExpression") = value
End Set
End Property
C# File: Departments.aspx.cs (excerpt)
protected void departmentsGrid_Sorting(object sender,
GridViewSortEventArgs e)
{
// Retrieve the name of the clicked column (sort expression)
string sortExpression = e.SortExpression;
// Decide and save the new sort direction
if (sortExpression == gridSortExpression)
{
if(gridSortDirection == SortDirection.Ascending)
{
gridSortDirection = SortDirection.Descending;
}
else
{
gridSortDirection = SortDirection.Ascending;
}
}
else
511
Implementing Sorting
{
gridSortDirection = SortDirection.Ascending;
}
// Save the new sort expression
gridSortExpression = sortExpression;
// Rebind the grid to its data source
BindGrid();
}
private SortDirection gridSortDirection
{
get
{
// Initial state is Ascending
if (ViewState["GridSortDirection"] == null)
{
ViewState["GridSortDirection"] = SortDirection.Ascending;
}
// Return the state
return (SortDirection) ViewState["GridSortDirection"];
}
set
{
ViewState["GridSortDirection"] = value;
}
}
private string gridSortExpression
{
get
{
// Initial sort expression is DepartmentID
if (ViewState["GridSortExpression"] == null)
{
ViewState["GridSortExpression"] = "DepartmentID";
}
// Return the sort expression
return (string) ViewState["GridSortExpression"];
}
set
{
ViewState["GridSortExpression"] = value;
}
}
512
Chapter 12: Advanced Data Access

Get Build Your Own ASP.NET 2.0 Web Site Using C# & VB, Second Edition now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.