<%@ Register TagPrefix="Title" TagName="TitleShow" Src="/aspnetdata/Title.ascx" %>
<%@Page Debug=True %>
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.SqlClient" %>
<%@ Import Namespace="System.Web" %>
<html>
<head>
	<script language="C#" runat="server">
	SqlConnection Source;
	string NowSort;
	string d="SELECT Staff.*, MonthPay.* FROM MonthPay INNER JOIN Staff ON MonthPay.SID = Staff.SID Order by Staff.SID";
	void Page_load(object sender,EventArgs e){
		Source = (SqlConnection)Session["NowEmpSQL"];
		if(IsPostBack==false){
        		Hashtable HT=new Hashtable();
	        	HT.Add("StaffID", "SID");
        		HT.Add("Name", "cname");
        		HT.Add("PID", "pid");
        		HT.Add("YearMonth", "YearMonth");
        		HT.Add("Pay", "Pay");
			AllField.DataSource = HT;
			AllField.DataValueField="Value";
			AllField.DataTextField="Key";
			AllField.DataBind();
			NowSort="";
			bindNew(true);
			}
	}
	void bindNew(Boolean tag){//结合至DataGrid
		if(tag==true){
			DataSet DS = new DataSet();
			SqlDataAdapter NComm = new SqlDataAdapter(d, Source);
			NComm.Fill(DS, "staffPay");
			Session["NowDS"]=DS;//声明为Session
			staffPay.DataSource=DS.Tables["staffPay"].DefaultView;
			staffPay.DataBind();
		}
		else{
			DataSet DS=(DataSet)Session["NowDS"];
			DataView dv=DS.Tables["staffPay"].DefaultView;
			dv.Sort=NowSort;
			staffPay.DataSource=dv;
			staffPay.DataBind();
		}
	}
	void SortOK_Click(object sender,EventArgs e){
		DataSet DS=(DataSet)Session["NowDS"];
		DataView dv=DS.Tables["staffPay"].DefaultView;
		for(int i=0;i<AllField.Items.Count-1;i++){//取得每一选项
			if(AllField.Items[i].Selected==true){//判断是否选取
				NowSort +=  "," + AllField.Items[i].Value + " " + SortType.SelectedItem.Value;
			}
		}
		if(NowSort==null){//若未选取则不处理
			return;
		}
		else{
			NowSort=NowSort.Remove(0,1);
			dv.Sort = NowSort; //执行排序
			staffPay.DataSource=dv;
			staffPay.DataBind();
		}
	}
	</script>
</head>
<body background="/aspnetdata/images/winback.gif">
<Title:TitleShow id="BookT" Runat="server" />
<form runat="server" >
<table><tr><td valign="top" width="70">
指定欲排序的字段(可复选)：
</td><td><asp:ListBox id=AllField Width="100px" rows="4" runat="server" selectionmode="Multiple" /></td>
<td valign="top">
　排序方式：<asp:DropDownlist ID="SortType" Runat="Server" >
            <asp:ListItem value="asc" selected>递增</asp:ListItem>
            <asp:ListItem value="desc">递减</asp:ListItem>
</asp:DropDownList>
<asp:Button id="SortOK" Runat="Server" Onclick="SortOK_Click" Text="执行排序" />
</td>
</tr></table>
      <ASP:DataGrid id="staffPay" runat="server"
        BorderColor="black"
        BorderWidth="1"
        GridLines="Both"
        CellPadding="3"
        CellSpacing="0"
        Font-Name="Verdana"
        Font-Size="9pt"
        HeaderStyle-BackColor="LightYellow"
	HeaderStyle-HorizontalAlign="Center"
	DataKeyField="SID"
	AutoGenerateColumns="FALSE"
	SelectedItemStyle-BackColor="#FFFFC0"
>
      <Columns>
	<asp:BoundColumn HeaderText="StaffID" DataField="SID" ItemStyle-Wrap="false"  />
	<asp:BoundColumn HeaderText="Name" DataField="cname" />
	<asp:BoundColumn HeaderText="PID" DataField="pid" />
	<asp:BoundColumn HeaderText="YearMonth" DataField="YearMonth" />
	<asp:TemplateColumn HeaderText="Pay" >
		<ItemTemplate>
			<asp:Label runat="server" Text=<%# DataBinder.Eval(Container.DataItem, "Pay","{0:c}") %> id="Pay"/>
		</ItemTemplate>
	</asp:TemplateColumn>
	</Columns>
</ASP:DataGrid>
</form>