GridView點擊排序時帶小圖片
排序
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.OleDb;
public partial class Sorting : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
this.BindGridView();
}
}
/// <summary>
/// 綁定gridview
/// </summary>
public void BindGridView()
{
string sql = "select * from tb_test";
//獲取gridview排序的數據列及其方向
string sortExpression = this.GridView1.Attributes["SortExpression"];
string sortDirection = this.GridView1.Attributes["SortDirection"];
//調用方法
DataTable dt = new DataTable();
dt = OracleHelper.getDB(sql);
if ((!string.IsNullOrEmpty(sortExpression)) && (!string.IsNullOrEmpty(sortDirection)))
{
dt.DefaultView.Sort = string.Format("{0} {1}", sortExpression, sortDirection);
}
//GridView綁定并顯示數據
this.GridView1.DataSource = dt;
this.GridView1.DataBind();
}
/// <summary>
/// 排序
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void GridView1_Sorting(object sender, GridViewSortEventArgs e)
{
//從事件參數中獲取排序的數據列
string sortExpression = e.SortExpression.ToString();
//假定排序方向為‘順序’
string sortDirection = "ASC";
//'ASC' 與從時間參數獲取的排序方向相比較,進行gridview排序方向的修改
if (sortExpression == this.GridView1.Attributes["SortExpression"])
{
//獲取下一次排序狀態
sortDirection = (this.GridView1.Attributes["SortDirection"].ToString() == sortDirection ? "DESC" : "ASC");
}
// 重新设定GridView排序数据列及排序方向
this.GridView1.Attributes["SortExpression"] = sortExpression;
this.GridView1.Attributes["SortDirection"] = sortDirection;
this.BindGridView();
GridViewRow headerRow = GridView1.HeaderRow;
Image sortImage = new Image();
if (sortDirection == "ASC")
{
sortImage.ImageUrl = "images/hmenu-asc.gif";
sortImage.Style.Add("vertical-align", "bottom");
}
else
{
sortImage.ImageUrl = "images/hmenu-desc.gif";
sortImage.Style.Add("vertical-align", "bottom");
}
int num = 0;
foreach (DataControlField field in GridView1.Columns)
{
if (field.SortExpression == sortExpression.ToString().Trim())
{
num = GridView1.Columns.IndexOf(field);
}
}
headerRow.Cells[num].Controls.Add(sortImage);
}
}
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.OleDb;
public partial class Sorting : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
this.BindGridView();
}
}
/// <summary>
/// 綁定gridview
/// </summary>
public void BindGridView()
{
string sql = "select * from tb_test";
//獲取gridview排序的數據列及其方向
string sortExpression = this.GridView1.Attributes["SortExpression"];
string sortDirection = this.GridView1.Attributes["SortDirection"];
//調用方法
DataTable dt = new DataTable();
dt = OracleHelper.getDB(sql);
if ((!string.IsNullOrEmpty(sortExpression)) && (!string.IsNullOrEmpty(sortDirection)))
{
dt.DefaultView.Sort = string.Format("{0} {1}", sortExpression, sortDirection);
}
//GridView綁定并顯示數據
this.GridView1.DataSource = dt;
this.GridView1.DataBind();
}
/// <summary>
/// 排序
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void GridView1_Sorting(object sender, GridViewSortEventArgs e)
{
//從事件參數中獲取排序的數據列
string sortExpression = e.SortExpression.ToString();
//假定排序方向為‘順序’
string sortDirection = "ASC";
//'ASC' 與從時間參數獲取的排序方向相比較,進行gridview排序方向的修改
if (sortExpression == this.GridView1.Attributes["SortExpression"])
{
//獲取下一次排序狀態
sortDirection = (this.GridView1.Attributes["SortDirection"].ToString() == sortDirection ? "DESC" : "ASC");
}
// 重新设定GridView排序数据列及排序方向
this.GridView1.Attributes["SortExpression"] = sortExpression;
this.GridView1.Attributes["SortDirection"] = sortDirection;
this.BindGridView();
GridViewRow headerRow = GridView1.HeaderRow;
Image sortImage = new Image();
if (sortDirection == "ASC")
{
sortImage.ImageUrl = "images/hmenu-asc.gif";
sortImage.Style.Add("vertical-align", "bottom");
}
else
{
sortImage.ImageUrl = "images/hmenu-desc.gif";
sortImage.Style.Add("vertical-align", "bottom");
}
int num = 0;
foreach (DataControlField field in GridView1.Columns)
{
if (field.SortExpression == sortExpression.ToString().Trim())
{
num = GridView1.Columns.IndexOf(field);
}
}
headerRow.Cells[num].Controls.Add(sortImage);
}
}
OracleHelper.getDB()方法
public static DataTable getDB(string sql)
{
DataTable dt = new DataTable();
using (OleDbConnection conn = new OleDbConnection(ConnString))
{
OleDbDataAdapter comm = new OleDbDataAdapter(sql, conn);
DataSet ds = new DataSet();
try
{
conn.Open();
ds.Clear();
comm.Fill(ds);
dt = ds.Tables[0];
}
catch
{
conn.Close();
}
finally
{
conn.Close();
}
return dt;
}
}
{
DataTable dt = new DataTable();
using (OleDbConnection conn = new OleDbConnection(ConnString))
{
OleDbDataAdapter comm = new OleDbDataAdapter(sql, conn);
DataSet ds = new DataSet();
try
{
conn.Open();
ds.Clear();
comm.Fill(ds);
dt = ds.Tables[0];
}
catch
{
conn.Close();
}
finally
{
conn.Close();
}
return dt;
}
}