GridView排序问题

GridView排序问题解决

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

 

 

ContractedBlock.gifExpandedBlockStart.gifCode
 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 
13ExpandedBlockStart.gifContractedBlock.gif{
14    protected void Page_Load(object sender, EventArgs e)
15ExpandedSubBlockStart.gifContractedSubBlock.gif    {
16        if (!Page.IsPostBack)
17ExpandedSubBlockStart.gifContractedSubBlock.gif        {
18            this.BindData();
19        }

20    }

21    private DataTable GetData()
22ExpandedSubBlockStart.gifContractedSubBlock.gif    {
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()
27ExpandedSubBlockStart.gifContractedSubBlock.gif    {       
28        this.GridView1.DataSource = this.GetData();
29        this.GridView1.DataBind();
30    }

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

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

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

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

62        set
63ExpandedSubBlockStart.gifContractedSubBlock.gif        {
64            ViewState["sortDirection"= value;
65        }

66    }

67}

68

转载于:https://www.cnblogs.com/cykevin/articles/1285320.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值