您的位置:首页 > 其它

GridView带有上下箭头的多列排序

2008-08-21 09:38 399 查看
#region 私有变量

protected string JS = "";

string SortExpression = "";//排序表达式

string SortDirection = "";//排序方向

#endregion

#region 事件

/// <summary>

/// 生成排序列旁边的小箭头

/// </summary>

/// <param name="sender"></param>

/// <param name="e"></param>

protected void gvData_RowCreated(object sender, GridViewRowEventArgs e)

{

if (e.Row.RowType == DataControlRowType.Header)

{

foreach (TableCell tc in e.Row.Cells)

{

if (tc.Controls.Count > 0)//这里要判断一下此时是不是已经生成了linkbutton

{

string s1 = ((LinkButton)tc.Controls[0]).Text;

((LinkButton)tc.Controls[0]).Text = s1.Replace(s1, s1 + "<font face='Webdings'>5</font>");

}

if (tc.Controls.Count > 0 && tc.Controls[0].GetType().ToString() == "System.Web.UI.WebControls.DataControlLinkButton")

{

if (((LinkButton)tc.Controls[0]).CommandArgument == SortExpression)

{

string s2 = ((LinkButton)tc.Controls[0]).Text;

if (SortDirection == "DESC")

{

((LinkButton)tc.Controls[0]).Text = s2.Replace("5", "6");

}

}

}

}

}

}

/// <summary>

/// 排序方法

/// </summary>

/// <param name="sender"></param>

/// <param name="e"></param>

protected void gvData_Sorting(object sender, GridViewSortEventArgs e)

{

//得到排序条件

SortExpression = e.SortExpression.ToString();

//设置排序为升序

SortDirection = "ASC";

//当当前排序为升序时,修改成降序

if (this.gridView.Attributes["SortDirection"] == "ASC")

{

SortDirection = "DESC";

}

//设置GridView的排序状态

this.gridView.Attributes["SortExpression"] = SortExpression;

this.gridView.Attributes["SortDirection"] = SortDirection;

BindGridView();

}

#endregion

#region 私有方法

/// <summary>

/// 根据排序条件重新绑定GridView

/// </summary>

private void BindGridView()

{

string sortExpression = this.gridView.Attributes["SortExpression"];

string sortDirection = this.gridView.Attributes["SortDirection"];

if ((!string.IsNullOrEmpty(sortExpression)) && (!string.IsNullOrEmpty(sortDirection)))

{

DataSource.DefaultView.Sort = string.Format("{0} {1}", sortExpression, sortDirection);

}

this.gridView.DataSource = DataSource;

this.gridView.DataBind();

}

#endregion

以上就是全部的代码~
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: