グリッドビューの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
が返されますが、降順で並べ替えられます。
データソースとGridViewの間のデータバインディングに関して 'Page_Load'で何か起こっていますか? – ConnorsFan
いいえ。バインディングは、SelectedIndexChangedイベントでのみ引き起こされます。 – shiftingviews
ソート列を変更すると、同じ動作(ソート式が1つのポストバックが遅すぎますか)がありますか? – ConnorsFan