ASP.NET2.0中GRIDVIEW控件完全代码实现模版列排序!(代码调试已通过)

   protected   void  Page_Load( object  sender, EventArgs e)
 2      {
 3           if  ( ! IsPostBack)
 4          {
 5               // 初始化页面绑定数据到GridView
 6              GetDataSet();
 7              GridViewBind();
 8          }
 9      }  
10 
11       ///   <summary>
12       ///  生成ds数据集
13       ///   </summary>
14       private  DataSet GetDataSet()
15      {
16           // string strSQL = "SELECT * FROM MANAGER.DB1";
17           string  strSQL  =   " SELECT * FROM MANAGER.DB1 " ;
18          DataSet ds  =  conn.GetDs(strSQL);
19           return  ds;
20      }
21 
22       ///   <summary>
23       ///  绑定数据到GridView
24       ///   </summary>
25       private   void  GridViewBind()
26      {
27           this .GridView1.DataSource  =   this .GetDataSet().Tables[ 0 ].DefaultView;
28           this .GridView1.DataBind();
29      }
30       ///   <summary>
31       ///  存储选定列当前排序状态
32       ///   </summary>
33       public  SortDirection GridViewSortDirection
34      {
35           get
36          {
37               if  (ViewState[ " sortDirection " ==   null )
38                  ViewState[ " sortDirection " =  SortDirection.Ascending;
39               return  (SortDirection)ViewState[ " sortDirection " ];
40          }
41           set  { ViewState[ " sortDirection " =  value; }
42      }
43 
44      protected   void  GridView1_Sorting( object  sender, GridViewSortEventArgs e)
45     {
46       string  sortExpression  =  e.SortExpression;
47       if  (GridViewSortDirection  ==  SortDirection.Ascending)
48      {
49          GridViewSortDirection  =  SortDirection.Descending;
50          SortGridView(sortExpression,  " DESC " );
51      }
52       else
53      {
54          GridViewSortDirection  =  SortDirection.Ascending;
55          SortGridView(sortExpression,  " ASC " ); 
56      } 
57      }
58      
59       ///   <summary>
60       ///  排序并绑定
61       ///   </summary>
62       private   void  SortGridView( string  sortExpression,  string  direction)
63      {
64          DataTable dt  =   this .GetDataSet().Tables[ 0 ];
65          DataView dv  =   new  DataView(dt);
66 
67          dv.Sort  =  sortExpression  +   "   "   +  direction; 
68 
69          GridView1.DataSource  =  dv;
70          GridView1.DataBind();
71      }

本人在提供一个在单击排序时,后面出现向前排序还是向后排序,小箭头的代码,直接加入上面的代码中就可以使用!
同时只要在上面的
44 protected void GridView1_Sorting(object sender, GridViewSortEventArgs e)
45 {
46 string sortExpression = e.SortExpression;
加上这句: ViewState["SortExpression"]=sortExpression;
////////////////////////////////////////////////////////////////////////////
protected void GridView1_RowCreated(object sender, GridViewRowEventArgs e)
{
if (e.Row != null && e.Row.RowType == DataControlRowType.Header)
{
string strSortBy = (string)ViewState["SortExpression"];
string strSortAscending = null;
if (GridViewSortDirection == SortDirection.Descending)
strSortAscending = "desc";
else
strSortAscending = "asc";
string strOrder = (strSortAscending == "desc" ? "5" : "6");

for (int i = 0; i < GridView1.Columns.Count; i++)
{
if (strSortBy == GridView1.Columns[i].SortExpression)
{
//这里同样可以插入图片
Label lblSorted = new Label();
lblSorted.Font.Name = "webdings";
lblSorted.Font.Size = FontUnit.Small;
lblSorted.Text = strOrder;
e.Row.Cells[i].Controls.Add(lblSorted);
//这里没有这样做而是在单元格的控件集中在添加一个label控件(就形成了上下按钮),同样也可以添加一个图片按钮!
}
}
}

}

你可能感兴趣的:(GridView)