在DataGrid中,我们可以实现类似Windows资源管理器的效果,即对列进行排序,该列颜色与其它列不同。下面就是实现的代码:
C#代码:
DataGridLikeWindowsExplorer.ASPx
<%@ Page language="c#" Codebehind="DataGridLikeWindowsExplorer.aspx.cs"
AutoEventWireup="false" Inherits="aspx<a href="http://dev.21tx.com/web/" target="_blank">Web</a>CS.DataGridLikeWindowsExplorer" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
<HEAD>
<title>DataGridLikeWindowsExplorer</title>
<meta name="GENERATOR" Content="Microsoft Visual Studio 7.0">
<meta name="CODE_LANGUAGE" Content="C#">
<meta name="vs_defaultClientScript" content="<a href="http://dev.21tx.com/web/javascript/" target="_blank">JavaScript</a>">
<meta name="vs_targetSchema" content="http://schemas.microsoft.com/intellisense/ie5">
<style type="text/css">
.gridtooltip {
BORDER-RIGHT: black 1px solid;
PADDING-RIGHT: 4px;
BORDER-TOP: black 1px solid;
PADDING-LEFT: 4px;
Z-INDEX: 200;
LEFT: 0px;
VISIBILITY: hidden;
PADDING-BOTTOM: 4px;
FONT: 9pt bold 宋体;
BORDER-LEFT: black 1px solid;
WIDTH: 150px;
COLOR: white;
PADDING-TOP: 4px;
BORDER-BOTTOM: black 1px solid;
POSITION: absolute;
TOP: 0px;
BACKGROUND-COLOR: #ff3316
}
</style>
<script language="<a href="http://dev.21tx.com/java/" target="_blank">Java</a>script" type="text/javascript">
function showheadertip(idx)
{
var tooltip
if (document.getElementById)
tooltip = document.getElementById('htip' + idx)
else
tooltip = eval("document.all['htip" + idx + "']");
if (tooltip != null)
{
tooltip.style.pixelLeft = event.clientX + 5;
tooltip.style.pixelTop = event.clientY + 5;
tooltip.style.visibility = "visible";
}
}
function hideheadertip(idx)
{
var tooltip
if (document.getElementById)
tooltip = document.getElementById('htip' + idx)
else
tooltip = eval("document.all['htip" + idx + "']");
if (tooltip != null)
{
tooltip.style.visibility = "hidden";
}
}
</script>
</HEAD>
<body MS_POSITIONING="GridLayout">
<asp:PlaceHolder ID="Tips_PlaceHolder" Runat="server"></asp:PlaceHolder>
<form id="DataGridLikeWindowsExplorer" method="post" runat="server">
<div align="center">
<h4>Northwind职员表</h4>
<asp:DataGrid ID="WinExplorerView_DataGrid" Runat="server" BorderColor="#FF6600"
BorderStyle="None" BorderWidth="5px" BackColor="White" CellPadding="5" AllowSorting="True"
AutoGenerateColumns="False" AllowPaging="True" GridLines="Horizontal" PageSize="5">
<SelectedItemStyle Font-Bold="True" ForeColor="White" BackColor="#669999"></SelectedItemStyle>
<ItemStyle ForeColor="#000066"></ItemStyle>
<HeaderStyle Font-Bold="True" HorizontalAlign="Center" ForeColor="Navy" BackColor="#FFCC00"></HeaderStyle>
<FooterStyle ForeColor="#000066" BackColor="White"></FooterStyle>
<Columns>
<asp:BoundColumn DataField="LastName" SortExpression="LastName" HeaderText="姓"></asp:BoundColumn>
<asp:BoundColumn DataField="FirstName" SortExpression="FirstName" HeaderText="名字"></asp:BoundColumn>
<asp:BoundColumn DataField="Title" SortExpression="Title" HeaderText="职位"></asp:BoundColumn>
<asp:BoundColumn DataField="BirthDate" SortExpression="BirthDate" HeaderText="出生日期"
DataFormatString="{0:d}"/></asp:BoundColumn>
<asp:BoundColumn DataField="City" SortExpression="City" HeaderText="居住地"></asp:BoundColumn>
</Columns>
<PagerStyle HorizontalAlign="Left" ForeColor="#000066" BackColor="White" Mode="NumericPages"></PagerStyle>
</asp:DataGrid>
</div>
</form>
</body>
</HTML>
DataGridLikeWindowsExplorer.aspx.cs
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Web;
using System.Web.Caching;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
namespace aspxWebCS
{
/// <summary>
/// DataGridLikeWindowsExplorer 的摘要说明。
/// </summary>
public class DataGridLikeWindowsExplorer : System.Web.UI.Page
{
protected System.Web.UI.WebControls.DataGrid WinExplorerView_DataGrid;
protected System.Data.SqlClient.SqlDataAdapter m_sqlDataAdapter;
protected System.Data.SqlClient.SqlCommand m_sqlSelectCommand;
protected System.Data.SqlClient.SqlConnection m_sqlConnection;
protected System.Data.DataSet m_dsEmployees;
protected System.Data.DataView m_dvEmployees;
protected System.Collections.Hashtable colHeaderMap;
protected string m_strSortExpr;
protected string m_strSortOrder;
protected int m_iSortColumnIdx;
protected System.Web.UI.WebControls.PlaceHolder Tips_PlaceHolder;
protected string strConn = "Data Source=.;User Id=sa;Password=;Initial Catalog=Northwind;";
private void Page_Load(object sender, System.EventArgs e)
{
m_iSortColumnIdx = -1;
m_strSortExpr = "";
m_strSortOrder = "";
ProcessViewState();
PrepareColumnHeaderMap();
if (!IsPostBack)
{
BindGridToView();
}
}
private void PrepareColumnHeaderMap()
{
colHeaderMap = new Hashtable();
int idx = 0;
foreach (DataGridColumn col in WinExplorerView_DataGrid.Columns)
{
colHeaderMap[col.SortExpression] = idx++;
}
}
private void RetrieveData()
{
if (null == Cache["EmployeesDS"])
{
s