我遇到的情况是,当我第一次单击gridview的ItemID标题进行降序排序时,它仍然保持升序排序。它只在第二次点击时才会进行降序排序。这是我的代码:
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 code:
<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
第一次点击时但它实际上不会强制执行降序。然后在第二次点击时,它将返回
SELECT * FROM [MasterTbl]
ORDER BY ItemID ASC
然后它按降序排序。
答案 0 :(得分:0)
我已经复制了这个问题并尝试了许多方法来保持aggregate(CONC ~ ID + DRUG + FED, df, sum)
# ID DRUG FED CONC
#1 2 2 0 130
#2 1 1 1 60
DataSourceID="MyDataSource"
的声明,同时保持MyGridView
,但没有成功。
我能使其工作的唯一方法是从GridView的标记中删除DataSourceID
属性并在代码隐藏中设置数据源:
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();
}
这就是我使用数据绑定控件的数据源的方式,我从来没有经历过问题中描述的奇怪行为。