it之路。

GridView排序问题

GridView的Sorting事件,每次e.sortDirection都是升序排序,参考了下别人的解决方案

 

 


 1using System;
 2using System.Data;
 3using System.Configuration;
 4using System.Web;
 5using System.Web.Security;
 6using System.Web.UI;
 7using System.Web.UI.WebControls;
 8using System.Web.UI.WebControls.WebParts;
 9using System.Web.UI.HtmlControls;
10
11
12public partial class _Default : System.Web.UI.Page 
13{
14    protected void Page_Load(object sender, EventArgs e)
15    {
16        if (!Page.IsPostBack)
17        {
18            this.BindData();
19        }

20    }

21    private DataTable GetData()
22    {
23        const string sql_select_workorder = "select WorkOrderID,ProductID,OrderQty,StockedQty,ScrappedQty,StartDate,EndDate,DueDate,ScrapReasonID,ModifiedDate from Production.WorkOrder";
24        return DBUtility.SqlHelper.ExecuteDataSet(DBUtility.SqlHelper.workOrderConnectionString, sql_select_workorder).Tables[0];
25    }

26    private void BindData()
27    {       
28        this.GridView1.DataSource = this.GetData();
29        this.GridView1.DataBind();
30    }

31
32    protected void GridView1_Sorting(object sender, GridViewSortEventArgs e)
33    {
34        string sortExpression=e.SortExpression;
35        DataView dv = new DataView(GetData());
36        // 根据视图状态来取排序方向
37        if (GridViewSortDirection == SortDirection.Ascending)
38        {
39            // 如果本次是升序,那么下次将是倒序
40            GridViewSortDirection = SortDirection.Descending;
41            
42            dv.Sort = sortExpression + " desc";
43        }

44        else
45        {
46            // 如果本次是倒序,下次将是升序
47            GridViewSortDirection = SortDirection.Ascending;
48            dv.Sort = sortExpression;
49        }

50        this.GridView1.DataSource = dv;
51        this.GridView1.DataBind();
52    }

53    public SortDirection GridViewSortDirection
54    {
55        get 
56        {
57            // 第一次点击排序时,视图状态为空,将其以倒序排列
58            if (ViewState["sortDirection"== null)
59            { ViewState["sortDirection"= SortDirection.Ascending; }
60            return (SortDirection)ViewState["sortDirection"];
61        }

62        set
63        {
64            ViewState["sortDirection"= value;
65        }

66    }

67}

68
posted @ 2008-09-05 17:45  3912.77  阅读(303)  评论(0编辑  收藏  举报