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

 1   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     }

posted on 2006-07-16 22:43  xh3  阅读(1869)  评论(6编辑  收藏  举报

导航