GridView标题分页,泛型作为数据源!
时间:2011-03-17 来源:rgzazq
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
#region 标题排序变量
ViewState["sortName"] = "CreateDate";
ViewState["sortOrder"] = "Desc";
#endregion
}
}
protected void grvDetail_Sorting(object sender, GridViewSortEventArgs e)
{
ViewState["sortName"] = e.SortExpression.ToString(); //排序字段
if (ViewState["sortOrder"].ToString() == "Desc")
ViewState["sortOrder"] = "Asc";
else
ViewState["sortOrder"] = "Desc";
this.BindData();
}
/// <summary>
/// 绑定数据
/// </summary>
private void BindData()
{
Bll_Brand bll = new Bll_Brand();
List<T_Brand> datalist = bll.GetBrandInfo();//你查询得到的泛型数据
#region 标题排序
var lst = datalist;
if (ViewState["sortName"] != null && ViewState["sortOrder"] != null)
{
string sortName = ViewState["sortName"].ToString().Trim();
string sortOrder = ViewState["sortOrder"].ToString().Trim();
PropertyInfo prop = typeof(T_Brand).GetProperty(sortName); //T_Brand是Model层的一个类
if (sortOrder == "Asc")
{
lst = datalist.OrderBy(T_Brand => prop.GetValue(T_Brand, null)).ToList();
}
else if (sortOrder == "Desc")
{
lst = datalist.OrderByDescending(T_Brand => prop.GetValue(T_Brand, null)).ToList();
}
}
else
{
Response.Write("<script>alert('标题排序所需条件部分丢失,当前为默认排序!');</script>");
}
if (lst != null)
{
rowcount = lst.Count;
}
#endregion
this.grvDetail.DataSource = lst;
this.grvDetail.DataBind();
}
OK 搞定!!!