用GirdView搭配SqlDataSource控制項時,只需要在GirdView上設定AllowSorting="True",就能有排序的功能。
** 1.拉SqlDataSource**
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:testConnectionString %>" SelectCommand="SELECT top 10 * FROM [A]"></asp:SqlDataSource>
** 2.拉Grid & set AutoSorting = true and DataSource**
<asp:GridView ID="GridView4" runat="server" AllowSorting="True" AutoGenerateColumns="False" DataSourceID="SqlDataSource1">
<Columns>
<asp:BoundField DataField="xxx" HeaderText="xxx" SortExpression="xxx" />
<asp:BoundField DataField="chk" HeaderText="chk" SortExpression="chk" />
<asp:BoundField DataField="chk2" HeaderText="chk2" SortExpression="chk2" />
<asp:CheckBoxField DataField="chk3" HeaderText="chk3" SortExpression="chk3" />
</Columns>
</asp:GridView>
1.拉GridView
**set GridView.AutoSorting = true **
add col & set DataField & SortExpression
<asp:GridView ID="GridView2" runat="server" AllowSorting="True" AutoGenerateColumns="False" OnSorting="GridView_Sorting">
<Columns>
<asp:BoundField DataField="xxx" HeaderText="xxx" SortExpression="xxx" />
<asp:BoundField DataField="chk" HeaderText="chk" SortExpression="chk"/>
<asp:BoundField DataField="chk2" HeaderText="chk2" SortExpression="chk2"/>
<asp:BoundField DataField="chk3" HeaderText="chk3" SortExpression="chk3"/>
</Columns>
</asp:GridView>
** 2.getdate & set sort**
private void GridView_DataBinding(GridView gv, string SortField, string SortDict)
{
string sConnstr = System.Configuration.ConfigurationManager.ConnectionStrings["testConnectionString"].ConnectionString;
string cmd = "select top 10 * from A ";
System.Data.DataTable dt = new connection.DBAccess().QueryDT(sConnstr, cmd); ;
//用dataview來做,但每次sort就要再跟DBquery一次,有沒有不需query的方法?!
System.Data.DataView dv = dt.DefaultView;
if (SortField != "")
{
string SortTxt = SortField;
if (SortDict == "1")
SortTxt += " DESC";
dv.Sort = SortTxt;
}
gv.DataSource = dv;
gv.DataBind();
}
** 3.GridView_Sorting -取目前sort狀態 **
protected void GridView_Sorting(object sender, GridViewSortEventArgs e)
{
//▲▼的code出自
// http://social.msdn.microsoft.com/Forums/zh-TW/236/thread/932a0f2b-f777-4621-950a-6ed3d7c39139
//Q:viewstate只有一個, 怎麼知道,這是哪個col的viewstate.
//A:它不知道,所以如果其它col desc了,這個col只能asc.
if (ViewState["SortDirection"] == null)
{
ViewState["SortDirection"] = SortDirection.Ascending.ToString();
}
//如果不要▲▼可以拿掉
for (int i = 0; i <= ((GridView)sender).Columns.Count - 1; i++)
{
((GridView)sender).Columns[i].HeaderText = ((GridView)sender).Columns[i].HeaderText.Replace("▲", "");
((GridView)sender).Columns[i].HeaderText = ((GridView)sender).Columns[i].HeaderText.Replace("▼", "");
}
for (int i = 0; i <= ((GridView)sender).Columns.Count - 1; i++)//有sortExpress的col,如果沒有設sortexpress,count就會是0
{
if (e.SortExpression == ((GridView)sender).Columns[i].SortExpression)
{
if (ViewState["SortDirection"].ToString() == SortDirection.Ascending.ToString())
{
e.SortDirection = SortDirection.Descending;
ViewState["SortDirection"] = SortDirection.Descending;
((GridView)sender).Columns[i].HeaderText = ((GridView)sender).Columns[i].HeaderText + "▼";//如果不要▲▼可以拿掉
}
else
{
e.SortDirection = SortDirection.Ascending;
ViewState["SortDirection"] = SortDirection.Ascending;
((GridView)sender).Columns[i].HeaderText = ((GridView)sender).Columns[i].HeaderText + "▲";//如果不要▲▼可以拿掉
}
}
}
string SortField = e.SortExpression;
string SortDir = e.SortDirection == SortDirection.Ascending ? "0" : "1";
GridView_DataBinding((GridView)sender, SortField, SortDir);
}
ref
https://dotblogs.com.tw/topcat/2009/02/23/7266 有介紹
https://dotblogs.com.tw/topcat/archive/2008/08/29/5248.aspx 範例