Asp.Net 的 GridView 有兩種資料綁定方法 DataSourceID 和 DataSource,以往DataSourceID的方法會自動幫你把排序/分頁處理好,但DataSource方法要自行手動一下。
以下這段是發表自行整合可以執行的程式碼,測試時務必自行變更BoundField段與SQL的資料取得。
// .ASPX File
<asp:GridView ID="GridView1" runat="server" AllowPaging="True" AutoGenerateColumns="False" DataKeyNames="ID"
Width="990px" BackColor="White" BorderColor="WhiteSmoke" BorderStyle="None" BorderWidth="0px" CellPadding="5"
Font-Names="Verdana" Font-Size="X-Small" ForeColor="Black" GridLines="Horizontal" PageSize="10" CssClass="myheader" AllowSorting="true" OnPageIndexChanging="GridView1_PageIndexChanging" OnSorting="GridView1_OnSorting">
<Columns>
<asp:BoundField DataField="ID" HeaderText="ID" SortExpression="ID" />
<asp:BoundField DataField="FirstName" HeaderText="First Name" SortExpression="FirstName" />
<asp:BoundField DataField="LastName" HeaderText="Last Name" SortExpression="LastName" />
<asp:BoundField DataField="Date" HeaderText="Date" SortExpression="Date" DataFormatString="{0:yyyy/MM/dd}"/>
</Columns>
<FooterStyle BackColor="#CCCC99" ForeColor="Black" />
<HeaderStyle Height="32px" Font-Bold="True" ForeColor="White" HorizontalAlign="Left" BorderStyle="None" />
<PagerStyle BackColor="White" ForeColor="Black" HorizontalAlign="Right" />
<SortedAscendingCellStyle BackColor="#F7F7F7" />
<SortedAscendingHeaderStyle BackColor="#4B4B4B" />
<SortedDescendingCellStyle BackColor="#E5E5E5" />
<SortedDescendingHeaderStyle BackColor="#242121" />
<RowStyle BorderColor="#f5f5f5" BorderStyle="Notset"/>
</asp:GridView>
此部分有自行結合Session以便不需要重新連線資料庫,執行於排序功能部分,分頁功能部分我也已經寫入(方法1 方法2),只是先註解掉。
//CODE BEHIND
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
GetDetails();
}
}
protected void GetDetails()
{
string strSql = "SELECT * FROM Test_Table Order by Date Desc ";
DataSet ds = DataProvider.Connect_Select(strSql);
Session["Dt01"] = ds.Tables[0]; // 將資料用DataTable方式加入 Session內
GridView1.DataSource = (DataTable)Session["Dt01"];
GridView1.DataBind();
}
protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
GridView gv = GridView1;
gv.PageIndex = e.NewPageIndex;
// 方法1 連線資料庫重新取資料
GetDetails();
// 方法2 Seeeion取資料
/*
DataTable dt = (DataTable)Session["Dt01"];
if (dt == null)
{
return;
}
else
{
gv.DataSource = dt;
gv.DataBind();
}
*/
}
// 排序時不需要再次連線資料庫取得資料,而是透過Session
protected void GridView1_OnSorting(object sender, GridViewSortEventArgs e)
{
GridView gv = GridView1;
// 這是我的個人習慣 如複雜時會用上 gv1 2 3 4的代稱或實際命名
DataTable dt = (DataTable)Session["Dt01"];
if (dt == null)
{
return;
}
else
{
DataView Dv = new DataView(dt);
Dv.Sort = e.SortExpression+" "+ GetSortDir(e.SortExpression);
gv.DataSource = Dv;
gv.DataBind();
}
}
private string GetSortDir(string column)
{
// By default, sort ascending
string sortDirection = "ASC";
// Check if the column is already sorted
string currentSortColumn = ViewState["Gv_SortCol"] as string;
if (currentSortColumn != null && currentSortColumn == column)
{
// Toggle the sort direction
string currentSortDirection = ViewState["Gv_SortDir"] as string;
if (currentSortDirection != null && currentSortDirection == "ASC")
{
sortDirection = "DESC";
}
}
// Save the sort column and direction in ViewState
ViewState["Gv_SortCol"] = column;
ViewState["Gv_SortDir"] = sortDirection;
return sortDirection;
}