Skip to main content

GridView Sort (排序)

GridView本身有一个Sort()函数:public virtual void Sort (
    string sortExpression,    SortDirection sortDirection)
使用上相当方便。

但在实际使用中,常用的是点击每一列的标题来排序,点击标题会触发:protected void GridView_Sorting(object sender, GridViewSortEventArgs e)事件。从GridViewSortEventArgs中可以拿到SortDirectionSortExpression。但是GridView.Sort()本身也会触发GridView_Sorting(),如果在GridView_Sorting()中调用GridView.Sort()会无限的循环,直到堆栈溢出。

这样就只能使用DataView.Sort属性来排序,再Bind到GridView上。

示例:protected string m_sortDirection
{
  get
  {
    if (ViewState["m_SortDirection"] == null)
  {
    return String.Empty;
  }
  return (string)ViewState["m_SortDirection"];
  }
    set { ViewState["m_SortDirection"] = value; }
}

protected string m_sortExpression
{
  get { return (string)ViewState["m_SortExpression"]; }
  set { ViewState["m_SortExpression"] = value; }
}

protected void bindData(string sortExpression, string sortDirection)
{
  DataView dv = new DataView(ds.Tables[0]);
  dv.Sort = sortExpression;
  if (sortDirection != String.Empty)
  {
    dv.Sort += " " + sortDirection;
 }

  gvRequisition.DataSource = dv;
  gvRequisition.DataBind();
}

protected void GridView_Sorting(object sender, GridViewSortEventArgs e)
{
  m_sortExpression = e.SortExpression;
  if (m_sortDirection == "ASC")
  {
    m_sortDirection = "DESC";
  }

  else if (m_sortDirection == "DESC")
  {
    m_sortDirection = "ASC";
  }
  bindData(m_sortExpression, m_sortDirection);
}

Comments

Anonymous said…
请问你有没有使用过Gridview.Sort()方法。QQ:390655561
cocoa said…
ds 是什么东西?
將王車 said…
to 祖元:
ds应该是个DataSet。这段代码是从实际项目中复制出来的,可能删除的时候误删了。

to Anonymous:
没有用过Gridview.Sort(),如果你让另一时间来触发Gridview.Sort()应该不会有什么问题。

Popular posts from this blog

asp.net Single Sign-On(SSO) from SAP

公司的主系统是SAP的东西,有的时候老板想要一些小的新功能或是某个部门需要一个自己使用的小系统又不想买SAP的东西(贵啊),只有自己写和使用一些第三方免费或是开源的系统,这个时候就难免涉及到 Single Sign-On 。而在asp.net 2.0下是非常容易实现的。有SAP提供的“Sapsecu.dll”,“sapssoext.dll”2个dll再加上由公司SAP系统提供的"verify.pse"就足够了。 第一步 复制Sapsecu.dll到system32目录,并使用Rersrv32注册。 第二步 在项目里添加对sapssoext.dll的引用,vs2005会自动生成Interop.SAPSSOEXT.dll来让.net使用。 第三步 在你项目的根目录创建sap目录,并将SAP系统那边提供的verify.pse复制过去。位置倒到不是一定要在这里,只要你找得到就好。 第四步 在SAP系统那边,需要生成一个类似:http://yourserver/login.aspx?sso=werwerwerwe的链接,当然具体的页面需要你和那边的管理员商量来决定。这个链接是有时效性的,我在做测试的时候他们给的测试链接通常只能用一天。现在我们要做到就是我们这边的编码,在login.aspx的page_load里面实现: protected void Page_Load(object sender, EventArgs e) {   string sso= Request.QueryString["sso"];   SAPSSOEXT.SSO2Ticket objSSO = new SAPSSOEXT.SSO2Ticket();   objSSO.CryptLib = "sapsecu.dll";   string strKeypath = Server.MapPath("~/sap/verify.pse");    //放在根目录比较容易找   object tt;   tt = objSSO.EvalLogonTicket(strTicket, strKeypath, String.Empty); ...

解决framework 4.5程序部署到Windows 8和windows 8.1出现“An app on your PC needs the following Windows feature: .NET Framework 3.5 (includes .NET 2.0 and 3.0)”

在windows 7 x64上编译的.net framework 4.5程序,使用InstallShiled打包以后安装到windows 8 && 8.1后出现 “An app on your PC needs the following Windows feature: .NET Framework 3.5 (includes .NET 2.0 and 3.0)” 的提示。 因为在windows8上默认只安装有.net framework 4.0 4.5,没有使用CLR 2.0的framework 2.0 3.0 3.5,而4.x使用的全新的CLR 4.0. 根据 how-to-solve-an-app-on-your-pc-needs-the-following-windows-feature-net-framework-3-5-includes-net-2-0-and-3-0-on-windows-8 这篇文章中提到相关资料,在exe.config中添加:

On Building

慢慢添东西,嘿嘿,放了个ico上去,美女哦,三国11的貂婵。 原计划是把整个css改为我自己设计的那个css的,仔细看了一下,我的和现在用这个根本没法比嘛。