

Navigator : Home > Tutorials > Controls Tutorials > ...
SortDirection of the GridView in ASP.NET 2.0 (VB)
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.
We migrated our web sites to Server Intellect over one weekend and the setup was so smooth that we were up and running right away. They assisted us with everything we needed to do for all of our applications. With Server Intellect's help, we were able to avoid any headaches!
First, you will need to import the System.Web.UI.WebControls namespace
| Imports System.Web.UI.WebControls |
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 |
The SortDirection.aspx page looks something like this:
Try Server Intellect for Windows Server Hosting. Quality and Quantity!
<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!