gridview 排序时候显示箭头

来源:互联网 发布:幸运星 山本宽 知乎 编辑:程序博客网 时间:2024/05/22 12:07


在asxc或aspx页面中的GridView中加入 如:

OnRowCreated="GridView1_RowCreated"




然后在cs文件中加入:
  1. //在GridView的RowCreated事件中  
  2.   
  3.   
  4.   
  5. protected void GridView1_RowCreated(object sender, GridViewRowEventArgs e)  
  6.   
  7.   
  8.   
  9. {   
  10.   
  11.   
  12.   
  13. if (e.Row.RowType == DataControlRowType.Header) //如果是表头  
  14.   
  15.   
  16.   
  17.     foreach (TableCell MyHeader in e.Row.Cells) //对每一单元格        
  18.   
  19.   
  20.   
  21.       if (MyHeader.HasControls())  
  22.   
  23.   
  24.   
  25.         if (((LinkButton)MyHeader.Controls[0]).CommandArgument == GridView1.SortExpression)  
  26.   
  27.         //是否为排序列  
  28.   
  29.   
  30.   
  31.           if (GridView1.SortDirection == SortDirection.Ascending) //依排序方向加入方向箭头  
  32.   
  33.   
  34.   
  35.             MyHeader.Controls.Add(new LiteralControl("↓"));  
  36.   
  37.   
  38.   
  39.           else  
  40.   
  41.   
  42.   
  43.             MyHeader.Controls.Add(new LiteralControl("↑"));  
  44.   
  45.   
  46.   


转自:http://blog.csdn.net/denuvead/archive/2008/07/03/2609447.aspx