2016-05-30 7 views
0

グリッドビューのItemIDヘッダーをクリックして降順ソートを初めて行うと、昇順ソートが維持されます。 2番目のクリックでのみ降順ソートが行われます。ここに私のコードは次のとおりです。最初のクリックでASP.NETのグリッドがソートされない

protected void MyDropDown_SelectedIndexChanged(object sender, EventArgs e) 
{ 
    this.ViewState["SortExpression"] = "ItemID"; 
    this.ViewState["SortDirection"] = "ASC"; 
    BindMyGrid(); 
} 

public void BindMyGrid() 
{   
    string sortExpression = this.ViewState["SortExpression"].ToString(); 
    string sortDirection = this.ViewState["SortDirection"].ToString(); 
    string strsql = "SELECT * FROM [MasterTbl] ORDER BY " + sortExpression + " " + sortDirection + ""; 
    MyDataSource.SelectCommand = strsql; 
} 

protected void MyGridView_Sorting(object sender, GridViewSortEventArgs e) 
{ 
    string sortExpression = this.ViewState["SortExpression"].ToString(); 
    string sortDirection = this.ViewState["SortDirection"].ToString(); 

    if (sortExpression == e.SortExpression) 
    { 
     this.ViewState["SortDirection"] = (sortDirection == "ASC") ? "DESC" : "ASC"; 
    } 
    else 
    { 
     this.ViewState["SortExpression"] = e.SortExpression; 
     this.ViewState["SortDirection"] = "ASC"; 
    } 

    this.BindMyGrid(); 
} 

HTMLコード:

<asp:GridView ID="MyGridView" runat="server" CssClass="table table-striped table-bordered table-hover" AutoGenerateColumns="False" 
      DataSourceID="MyDataSource" AllowPaging="true" onpageindexchanging="MyGridView_PageIndexChanging" OnRowDataBound="MyGridView_RowDataBound" 
      AllowSorting="true" OnSorting="MyGridView_Sorting" OnRowCreated="MyGridView_RowCreated"> 
    <Columns> 
      <%--Here is where my Boundfield columns are--%> 
    <Columns> 
    <PagerStyle CssClass="pagination-ys" /> 
</asp:GridView> 
<asp:SqlDataSource ID="MyDataSource" runat="server" ConnectionString="<%$ ConnectionStrings:DBCS %>"      
</asp:SqlDataSource> 

本当に私がバッフルは、私がデバッグモードに入るとき、STRSQLは、最初のクリックで

SELECT * FROM [MasterTbl] 
ORDER BY ItemID DESC 

を返しますが、それ実際に降順を実行することはありません。その後、2回目のクリックで、

SELECT * FROM [MasterTbl] 
ORDER BY ItemID ASC 

が返されますが、降順で並べ替えられます。

+0

データソースとGridViewの間のデータバインディングに関して 'Page_Load'で何か起こっていますか? – ConnorsFan

+0

いいえ。バインディングは、SelectedIndexChangedイベントでのみ引き起こされます。 – shiftingviews

+0

ソート列を変更すると、同じ動作(ソート式が1つのポストバックが遅すぎますか)がありますか? – ConnorsFan

答えて

0

DataSourceID="MyDataSource"MyGridViewの宣言にそのまま残して、問題を再現して正しい動作を得るために多くのことを試しました。私は、データソースを使用する方法です

public void BindMyGrid() 
{   
    string sortExpression = this.ViewState["SortExpression"].ToString(); 
    string sortDirection = this.ViewState["SortDirection"].ToString(); 
    string strsql = "SELECT * FROM [MasterTbl] ORDER BY " + sortExpression + " " + sortDirection + ""; 
    MyDataSource.SelectCommand = strsql; 
    MyGridView.DataSource = MyDataSource; 
    MyGridView.DataBind(); 
} 

私はそれを動作させることができる唯一の方法は、GridViewコントロールのマークアップからDataSourceID属性を削除すると、コードビハインドでデータソースを設定することですデータバインドされたコントロールと私は質問に記載された奇妙な動作を経験したことがない。

+0

ああ、それは私がする必要があったものです。私はマークアップからDataSourceIDを削除しました。私はそれが簡単な小さな見落としであることを知っていた。どうもありがとうございました! – shiftingviews

関連する問題