DotNet Tutorials

V4 Dot Net Tutorials

Server Intellect Cloud Hosting

 SortDirection of the GridView in ASP.NET 2.0 (C#)

This tutorial will demonstrate how to use the SortDirection enumeration in ASP.NET 2.0 and C# 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.

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!

First, you will need to import the System.Web.UI.WebControls  namespace

using System.Web.UI.WebControls;

We use the CustomersGridView_DataBound event to do the work

public void CustomersGridView_DataBound(Object sender, EventArgs e)
{
// Get the header row.
GridViewRow headerRow = CustomersGridView.HeaderRow;

// Get the footer row.
GridViewRow footerRow = CustomersGridView.FooterRow;

// Set the font color of the header and footer rows
// based on the sort direction.
switch (CustomersGridView.SortDirection)
{
case SortDirection.Ascending:
headerRow.ForeColor = System.Drawing.Color.Green;
footerRow.ForeColor = System.Drawing.Color.Green;
break;
case SortDirection.Descending:
headerRow.ForeColor = System.Drawing.Color.Red;
footerRow.ForeColor = System.Drawing.Color.Red;
break;
default:
headerRow.ForeColor = System.Drawing.Color.Black;
footerRow.ForeColor = System.Drawing.Color.Black;
break;
}
// Display the sort order in the footer row.
footerRow.Cells[0].Text = "Sorting in " + CustomersGridView.SortDirection.ToString()+" Order";
}

The SortDirection.aspx page looks something like this:

If you're ever in the market for some great Windows web hosting, try Server Intellect. We have been very pleased with their services and most importantly, technical support.

<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 VB.NET 2005 Version? Click Here!

Looking for more ASP.NET Tutorials? Click Here!

Download Project Source - Enter your Email to be emailed a link to download the Full Source Project used in this Tutorial!



100% SPAM FREE! We will never sell or rent your email address!