Server Intellect
 
Home   Asp.Net Tutorials   What's New   Newsletter   More Resources
 
 
  Categories
Advanced Technologies
AJAX
Internet Browsers
Controls
Database
Email
Error Handling
File
Graphics
Website Navigation
Network
Performance
User Interface and Themes
Validation
Visual Web Developer
Web Services
XML
Suggest Tutorial


Navigator: Home - Tutorials - Controls - SortDirection of the GridView in ASP.NET 2.0 (C#)
SortDirection of the GridView in ASP.NET 2.0 (C#)


ASP.NET Controls Tutorial

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.

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

using System.Web.UI.WebControls;

We moved our web sites to Server Intellect and have found them to be incredibly professional. Their setup is very easy and we were up and running in no time.

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";
}

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!

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>

Download the Full Working Version of this Project written with Visual Studio.NET C# 2005 Here!

Looking for the VB.NET 2005 Version? Click Here!

Looking for more ASP.NET Tutorials? Click Here!







 
  Developer Resources







Server Intellect Rocks