This tutorial will demonstrate how to use the SortDirection enumeration in ASP.NET and VB.NET 2.0 to determine the direction of items displayed by GridView control.
The SortDirection enumeration is used to represent the direction in which items are sorted.It is commonly used by properties (such as the SortDirection property of the GridView class) to indicate the order in which items are displayed in a control.
The following code example demonstrates how to use the SortDirection enumeration to determine the direction in which the GridView control is displaying its items. The MS sample database of Northwind is required for this sample.
First, you will need to import the System.Web.UI.WebControls namespace
| Imports System.Web.UI.WebControls |
If you're looking for a really good web host, try Server Intellect - we found the setup procedure and control panel, very easy to adapt to and their IT team is awesome!
We use the CustomersGridView_DataBound event to do the work
Sub CustomersGridView_DataBound(ByVal sender As Object, ByVal e As EventArgs)
' Get the header row.
Dim headerRow As GridViewRow = CustomersGridView.HeaderRow
' Get the footer row.
Dim footerRow As GridViewRow = CustomersGridView.FooterRow
' Set the font color of the header and footer rows
' based on the sort direction.
Select Case CustomersGridView.SortDirection
Case WebControls.SortDirection.Ascending
headerRow.ForeColor = System.Drawing.Color.Green
footerRow.ForeColor = System.Drawing.Color.Green
Case WebControls.SortDirection.Descending
headerRow.ForeColor = System.Drawing.Color.Red
footerRow.ForeColor = System.Drawing.Color.Red
Case Else
headerRow.ForeColor = System.Drawing.Color.Black
footerRow.ForeColor = System.Drawing.Color.Black
End Select
' Display the sort order in the footer row.
footerRow.Cells(0).Text = "Sorting in " & CustomersGridView.SortDirection.ToString() + " order"
End Sub
|
Yes, it is possible to find a good web host. Sometimes it takes a while. After trying several, we went with Server Intellect and have been very happy. They are the most professional, customer service friendly and technically knowledgeable host we've found so far.
The SortDirection.aspx page looks something like this:
<html>
<body>
<form runat="server">
<h3>GridView HeaderRow Example</h3>
<asp:gridview id="CustomersGridView"
datasourceid="CustomersSource"
autogeneratecolumns="true"
emptydatatext="No data available."
allowsorting="true"
allowpaging="true"
showheader="true"
showfooter="true"
ondatabound="CustomersGridView_DataBound"
runat="server">
<headerstyle backcolor="LightCyan"
forecolor="MediumBlue"/>
<footerstyle backcolor="LightCyan"
forecolor="MediumBlue"/>
</asp:gridview>
<!-- This example uses Microsoft SQL Server and connects -->
<!-- to the Northwind sample database. Use an ASP.NET -->
<!-- expression to retrieve the connection string value -->
<!-- from the Web.config file. -->
<asp:sqldatasource id="CustomersSource"
selectcommand="Select [CustomerID], [CompanyName], [Address], [City], [PostalCode], [Country] From [Customers]"
connectionstring="<%$ ConnectionStrings:NorthWindConnectionString%>"
runat="server"/>
</form>
</body>
</html>
|
Looking for the C#.NET 2005 Version? Click Here!
Looking for more ASP.NET Tutorials? Click Here!