DataGrid双向排序以及给HeaderText加图标
DataGrid默认情况下只支持单向排序,而且默认是升序(ASC)。能不能使之支持双向排序并且给HeaderText加图标呢?
下面的例子将演示如何给DataGrid动态添加sortexpression 和 sortdirection 属性,并通过DataView使DataGird中的数据按照这两个属性排列。
假设在页面上添加了一个ID=“showdata”的DataGrid。使用Northwind数据库中的customers表来说明这个例子。
首先,我们必须知道:
(1).数据源一定是Dataview,因为DataView提供了一种过滤和排序DataTable中数据的一种方式。
(2).DataGrid的AllowSorting属性要为True。
(3).DataGrid的AutogenerateColumns属性要为False
(4).添加要绑定的字段到DataGrid,并设置Sortexpression属性和数据库中数据表中的字段名保持一致。
下面的例子将演示如何给DataGrid动态添加sortexpression 和 sortdirection 属性,并通过DataView使DataGird中的数据按照这两个属性排列。
假设在页面上添加了一个ID=“showdata”的DataGrid。使用Northwind数据库中的customers表来说明这个例子。
首先,我们必须知道:
(1).数据源一定是Dataview,因为DataView提供了一种过滤和排序DataTable中数据的一种方式。
(2).DataGrid的AllowSorting属性要为True。
(3).DataGrid的AutogenerateColumns属性要为False
(4).添加要绑定的字段到DataGrid,并设置Sortexpression属性和数据库中数据表中的字段名保持一致。
//在页面第一次被加载时给DataGrid添加动态属性sortexpression 和 sortdirection,
//给指定的字段的sortexpression 和 sortdirection 赋值,在加载时初始化DataGrid的排序字段和排序方式
protected void Page_Load(object sender, System.EventArgs e)
{
if (!IsPostBack)
{
if (showdata.Attributes["SortExpression"] == null)
{
showdata.Attributes["SortExpression"] = "CustomerID";
showdata.Attributes["SortDirection"] = "ASC";
}
BindGrid();
}
}
//给指定的字段的sortexpression 和 sortdirection 赋值,在加载时初始化DataGrid的排序字段和排序方式
protected void Page_Load(object sender, System.EventArgs e)
{
if (!IsPostBack)
{
if (showdata.Attributes["SortExpression"] == null)
{
showdata.Attributes["SortExpression"] = "CustomerID";
showdata.Attributes["SortDirection"] = "ASC";
}
BindGrid();
}
}
// 得到DataSet 和 DataView 对象。设置DataView 的sort 属性为动态属性sortexpression 和 sortdirection的连接字符串
private void BindGrid()
{
SqlConnection conn = new SqlConnection("server=localhost;uid=sa;pwd=sa;database=Northwind");
conn.Open();
SqlDataAdapter cmd = new SqlDataAdapter("select * from customers", conn);
DataSet ds = new DataSet();
cmd.Fill(ds, "customers");
DataView dv = new DataView(ds);
string SortExpression = showdata.Attributes["SortExpression"];
string SortDirection = showdata.Attributes["SortDirection"];
dv.Sort = SortExpression + " " + SortDirection;
this.showdata.DataSource = dv;
this.showdata.DataBind();
}
private void BindGrid()
{
SqlConnection conn = new SqlConnection("server=localhost;uid=sa;pwd=sa;database=Northwind");
conn.Open();
SqlDataAdapter cmd = new SqlDataAdapter("select * from customers", conn);
DataSet ds = new DataSet();
cmd.Fill(ds, "customers");
DataView dv = new DataView(ds);
string SortExpression = showdata.Attributes["SortExpression"];
string SortDirection = showdata.Attributes["SortDirection"];
dv.Sort = SortExpression + " " + SortDirection;
this.showdata.DataSource = dv;
this.showdata.DataBind();
}
//不要图标的SortCommand事件
private void showdata_SortCommand(object source, System.Web.UI.WebControls.DataGridSortCommandEventArgs e)
{
string SortExpression = e.SortExpression.ToString();
string SortDirection = "ASC";
if (SortExpression == showdata.Attributes["SortExpression"])
{
SortDirection = (showdata.Attributes["SortDirection"].ToString() == SortDirection ? "DESC" : "ASC");
}
showdata.Attributes["SortExpression"] = SortExpression;
showdata.Attributes["SortDirection"] = SortDirection;
BindGrid();
}
private void showdata_SortCommand(object source, System.Web.UI.WebControls.DataGridSortCommandEventArgs e)
{
string SortExpression = e.SortExpression.ToString();
string SortDirection = "ASC";
if (SortExpression == showdata.Attributes["SortExpression"])
{
SortDirection = (showdata.Attributes["SortDirection"].ToString() == SortDirection ? "DESC" : "ASC");
}
showdata.Attributes["SortExpression"] = SortExpression;
showdata.Attributes["SortDirection"] = SortDirection;
BindGrid();
}
//要图标的SortCommand事件,当表头被点击时,触发DataGrid的sortcommand事件,得到sortcommand事件传递过来的sortexpression属性,并与datagrid的sortexpression属性比较,再比较SortDirection……
protected void showdata_SortCommand(object source, DataGridSortCommandEventArgs e)
{
string ImgDown = "<img border=0 src=" + Request.ApplicationPath + "/uparrow.gif>";
string ImgUp = "<img border=0 src=" + Request.ApplicationPath + "/downarrow.gif>";
string SortExpression = e.SortExpression.ToString();
string SortDirection = "ASC";
int colindex = -1;
//清空之前的图标
for (int i = 0; i < showdata.Columns.Count; i++)
{
showdata.Columns[i].HeaderText = (showdata.Columns[i].HeaderText).ToString().Replace(ImgDown, "");
showdata.Columns[i].HeaderText = (showdata.Columns[i].HeaderText).ToString().Replace(ImgUp, "");
}
//找到所点击的HeaderText的索引号
for (int i = 0; i < showdata.Columns.Count; i++)
{
if (showdata.Columns[i].SortExpression == e.SortExpression)
{
colindex = i;
break;
}
}
if (SortExpression == showdata.Attributes["SortExpression"])
{
SortDirection = (showdata.Attributes["SortDirection"].ToString() == SortDirection ? "DESC" : "ASC");
}
showdata.Attributes["SortExpression"] = SortExpression;
showdata.Attributes["SortDirection"] = SortDirection;
if (showdata.Attributes["SortDirection"] == "ASC")
{
showdata.Columns[colindex].HeaderText = showdata.Columns[colindex].HeaderText + ImgDown;
}
else
{
showdata.Columns[colindex].HeaderText = showdata.Columns[colindex].HeaderText + ImgUp;
}
BindGrid();
}
protected void showdata_SortCommand(object source, DataGridSortCommandEventArgs e)
{
string ImgDown = "<img border=0 src=" + Request.ApplicationPath + "/uparrow.gif>";
string ImgUp = "<img border=0 src=" + Request.ApplicationPath + "/downarrow.gif>";
string SortExpression = e.SortExpression.ToString();
string SortDirection = "ASC";
int colindex = -1;
//清空之前的图标
for (int i = 0; i < showdata.Columns.Count; i++)
{
showdata.Columns[i].HeaderText = (showdata.Columns[i].HeaderText).ToString().Replace(ImgDown, "");
showdata.Columns[i].HeaderText = (showdata.Columns[i].HeaderText).ToString().Replace(ImgUp, "");
}
//找到所点击的HeaderText的索引号
for (int i = 0; i < showdata.Columns.Count; i++)
{
if (showdata.Columns[i].SortExpression == e.SortExpression)
{
colindex = i;
break;
}
}
if (SortExpression == showdata.Attributes["SortExpression"])
{
SortDirection = (showdata.Attributes["SortDirection"].ToString() == SortDirection ? "DESC" : "ASC");
}
showdata.Attributes["SortExpression"] = SortExpression;
showdata.Attributes["SortDirection"] = SortDirection;
if (showdata.Attributes["SortDirection"] == "ASC")
{
showdata.Columns[colindex].HeaderText = showdata.Columns[colindex].HeaderText + ImgDown;
}
else
{
showdata.Columns[colindex].HeaderText = showdata.Columns[colindex].HeaderText + ImgUp;
}
BindGrid();
}
这样的话,DataGrid就既能双向排序,在HeaderText中,又有眩图标了,嘿嘿。