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

Python中的self

习惯上,任何Python类方法的第一个参数(对当前实例d的引用)都叫做self。这个参数扮演着C++或Java中的保留字this的角色,但self在Python中并不是一个保留字,它只是一个命名习惯。虽然如此,也请除了self之外不要使用其它的名字,这是一个非常坚固的习惯。 出自:《Dive Into Python》

Google Notebook整合了Google 书签

FF上一直装有Google笔记本的插件,一直没有到一面上去看,今天无意点到。发现多了个叫“Unfiled”的记事本,记得自己并未建过这么个东西,进去一看才发现,其实就是我的Google书签: 并且右下角也多了“Labels”,且与我Google书签中的相同。 其实人家的右上角的“ New features! ”里说得很清楚: Integration with Google Bookmarks Notebook is now integrated with Google Bookmarks. Your bookmarks will show up as a special Unfiled notebook, making Google Notebook a single place to collect and organize interesting web pages. To publish and collaborate, just drag and drop bookmarks into notebooks.

sp_tables的@table_type参数

打算写个工具,直接从数据库按字段生成类,使用到了sp_tables,遇到了很奇怪的问题。 查MSDN可以知道:sp_tables的参数之一:@table_type varchar(100) 可选项有: TABLE 、 SYSTEMTABLE 、 VIEW 。 我写了如下代码: Databasedb = DatabaseFactory.CreateDatabase(); DbCommand com = new SqlCommand(); com.CommandText = "sp_tables"; com.CommandType = CommandType.StoredProcedure; com.Parameters.AddWithValue("@table_type", "TABLE"); IDataReader reader = db.ExecuteReader(com); 却拿不到任何东西。不加@talbe_type的话又返回了大量的系统VIEW。 查看sp_tables的源码,在关于@talbe_type的部分发现如下的东西: if (charindex('''TABLE''',@table_type) 0) 倒回去 仔细 看MSDN,参数的说明部分: [ , [ @table_type = ] "'type'" ] 比一般的多了2个",而且下面也有说明 Single quotation marks must enclose each table type, and double quotation marks must enclose the whole parameter. Table types must be uppercase. If SET QUOTED_IDENTIFIER is ON, each single quotation mark must be doubled and the whole parameter must be enclosed in single quotation marks. 太大意了。 修改代码: com.Parameters.AddWithValue(...