***********前台代码:*************
<%@ Page language="c#" Codebehind="employee_view.aspx.cs" AutoEventWireup="false" Inherits="softweb.employee.employee_view" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
<HEAD>
<title>employee_view</title>
<meta content="Microsoft Visual Studio .NET 7.1" name="GENERATOR">
<meta content="C#" name="CODE_LANGUAGE">
<meta content="JavaScript" name="vs_defaultClientScript">
<meta content="http://schemas.microsoft.com/intellisense/ie5" name="vs_targetSchema">
</HEAD>
<body background="../images/rose_v5.gif">
<form id="Form1" method="post" runat="server">
<TABLE id="Table1" cellSpacing="0" cellPadding="0" width="100%" align="center" border="0">
<TR>
<TD align="center"><asp:label id="labView" runat="server" Font-Bold="True" Font-Size="26px" ForeColor="DarkCyan"
Height="40px" Width="192px">员 工 查 看</asp:label></TD>
</TR>
<TR>
<TD align="center"><asp:datagrid id="DataGrid1" runat="server" Height="152px" Width="770px" AutoGenerateColumns="False"
BorderWidth="1px" BorderColor="#CC6633" AllowSorting="True">
<ItemStyle HorizontalAlign="Center"></ItemStyle>
<HeaderStyle HorizontalAlign="Center" ForeColor="#003366" BackColor="#CCFFFF"></HeaderStyle>
<Columns>
<asp:BoundColumn Visible="False" DataField="employeeID" HeaderText="id"></asp:BoundColumn>
<asp:TemplateColumn HeaderText="序号">
<HeaderStyle Width="50px"></HeaderStyle>
<ItemTemplate>
<%# xuhao++%>
</ItemTemplate>
</asp:TemplateColumn>
<asp:BoundColumn DataField="employee_name" SortExpression="employee_name" HeaderText="员工姓名" DataFormatString="{0:c}">
<HeaderStyle Width="200px"></HeaderStyle>
</asp:BoundColumn>
<asp:BoundColumn DataField="in_age" SortExpression="in_age" HeaderText="入职时间" DataFormatString="{0:yyyy-MM-dd}">
<HeaderStyle Width="150px"></HeaderStyle>
</asp:BoundColumn>
<asp:BoundColumn DataField="sex" SortExpression="sex" HeaderText="性 别" DataFormatString="{0:c}">
<HeaderStyle Width="50px"></HeaderStyle>
</asp:BoundColumn>
<asp:BoundColumn DataField="user_name" SortExpression="user_name" HeaderText="用 户 名">
<HeaderStyle Width="300px"></HeaderStyle>
</asp:BoundColumn>
<asp:BoundColumn DataField="quanxian" SortExpression="quanxian" HeaderText="权 限">
<HeaderStyle Width="80px"></HeaderStyle>
</asp:BoundColumn>
</Columns>
</asp:datagrid></TD>
</TR>
<TR>
<TD align="center"></TD>
</TR>
</TABLE>
</form>
</body>
</HTML>
***********************后台代码*********************************
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
namespace softweb.employee
{
/// <summary>
/// employee_view 的摘要说明。
/// </summary>
public class employee_view : System.Web.UI.Page
{
protected System.Web.UI.WebControls.DataGrid DataGrid1;
protected System.Web.UI.WebControls.Label labView;
protected System.Data.SqlClient.SqlConnection myconn;
public int allcount;
public int xuhao;
//public int [] allcount = new int [10];
private void Page_Load(object sender, System.EventArgs e)
{
// 在此处放置用户代码以初始化页面
if(!this.IsPostBack)
{
this.DataGrid1.Attributes["SortExpression"]="employeeID";//默认排序字段
this.DataGrid1.Attributes["SrotDirection"]="ASC";//默认排序方式
string orderby=this.DataGrid1.Attributes["SortExpression"];
string sortdir=this.DataGrid1.Attributes["SrotDirection"];
this.DataGrid1.DataSource=this.bind(orderby,sortdir);//调用绑定函数
this.DataGrid1.DataBind();
}
}
//数据绑定
private DataView bind(string sortexpression,string sortdirection)
{
//数据库连接
DataView dv=new DataView();//定义数据表视图
xuhao=1;//自动序号排列
myconn=new SqlConnection(Session["strConn"].ToString());
myconn.Open();
SqlCommand cmd=new SqlCommand();
cmd.CommandType=CommandType.StoredProcedure;//查询语句为存储过程
cmd.CommandText="employee_view";
cmd.Connection=myconn;
SqlDataAdapter adpt=new SqlDataAdapter(cmd);
DataSet ds=new DataSet();
adpt.Fill(ds,"tab");
myconn.Close();
dv=ds.Tables["tab"].DefaultView;
dv.Sort=sortexpression+" "+sortdirection;//设置数据视图排序的方式
return dv; //返回一个数据表视图
}
#region Web 窗体设计器生成的代码
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
//
InitializeComponent();
base.OnInit(e);
}
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.DataGrid1.SortCommand += new System.Web.UI.WebControls.DataGridSortCommandEventHandler(this.DataGrid1_SortCommand);
this.Load += new System.EventHandler(this.Page_Load);
}
#endregion
//排序时触发事件
private void DataGrid1_SortCommand(object source, System.Web.UI.WebControls.DataGridSortCommandEventArgs e)
{
string orderby=e.SortExpression;
string orderdir="ASC";
if(orderby==this.DataGrid1.Attributes["SortExpression"])//如果仍为当前排序的字段(排序字段没有变)
{
//若当前为正排,则改为倒排序,否则反之。
orderdir=(this.DataGrid1.Attributes["SrotDirection"]==orderdir?"DESC":"ASC");
}
this.DataGrid1.Attributes["SortExpression"]=orderby;//保存当前的排序字段
this.DataGrid1.Attributes["SrotDirection"]=orderdir;//保存当前的排序顺序
this.DataGrid1.DataSource=this.bind(orderby,orderdir);
this.DataGrid1.DataBind();
}
}
}