问题
So I have sorting working but it only runs Ascending. When I click it again just runs Ascending still. I debugged it and the sortDirection never changes. here is my sort code. I looked into using viewstate but couldn't find a clean way of using it for this situation.
private string ConvertSortDirectionToSql(SortDirection sortDirection)
{
string newSortDirection = String.Empty;
switch (sortDirection)
{
case SortDirection.Ascending:
newSortDirection = "ASC";
break;
case SortDirection.Descending:
newSortDirection = "DESC";
break;
}
return newSortDirection;
}
protected void caseloads_Sorting(object sender, GridViewSortEventArgs e)
{
DataTable dataTable = caseloads.DataSource as DataTable;
if (dataTable != null)
{
DataView dataView = new DataView(dataTable);
dataView.Sort = e.SortExpression + " " + ConvertSortDirectionToSql(e.SortDirection);
caseloads.DataSource = dataView;
caseloads.DataBind();
}
}
回答1:
So I went with view state. It sets the beginning to Ascending so you can even change it to Descending if you wish to do that. The caseLoads(); is where my gridview is stored and pulling all the information.
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
ViewState["sorting"] = "Ascending";
}
caseLoads();
}
then in the sorting we have if statement that are looking for Ascending and Descending.
private string ConvertSortDirectionToSql(SortDirection sortDirection)
{
string newSortDirection = String.Empty;
string sort = Convert.ToString(ViewState["sorting"]);
if (sort == "Ascending")
{
newSortDirection = "ASC";
ViewState["sorting"] = "Descending";
}
else if (sort == "Descending")
{
newSortDirection = "DESC";
ViewState["sorting"] = "Ascending";
}
return newSortDirection;
}
Here is the gridview sort
protected void caseloads_Sorting(object sender, GridViewSortEventArgs e)
{
DataTable dataTable = caseloads.DataSource as DataTable;
if (dataTable != null)
{
DataView dataView = new DataView(dataTable);
dataTable = dataView.ToTable();
dataView.Sort = e.SortExpression + " " + ConvertSortDirectionToSql(e.SortDirection);
caseloads.DataSource = dataView;
caseloads.DataBind();
}
}
Here is the gridview I am using in asp
<asp:GridView ID="caseloads" runat="server" AutoGenerateColumns="false" GridLines="None"
Font-Size="12.5px" BackColor="#FFFFFF" CssClass="mGrid" OnSorting="caseloads_Sorting" AllowSorting="true">
<Columns>
<asp:TemplateField HeaderText="Client ID" SortExpression="ClientKey">
<ItemTemplate>
<asp:Label ID="clientKey1" runat="server" Text='<%#Eval("ClientKey")%>' />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Full Name" SortExpression="ConsumerName">
<ItemTemplate>
<asp:Label ID="clientKey2" runat="server" Text='<%#Eval("ConsumerName")%>' />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Program" SortExpression="info">
<ItemTemplate>
<asp:Label ID="clientKey3" runat="server" Text='<%#Eval("info")%>' />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="House" SortExpression="phoneH">
<ItemTemplate>
<asp:Label ID="clientKey4" runat="server" Text='<%#Eval("phoneH")%>' />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Cell" SortExpression="phoneC">
<ItemTemplate>
<asp:Label ID="clientKey5" runat="server" Text='<%#Eval("phoneC")%>' />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
来源:https://stackoverflow.com/questions/33112464/gridview-only-asc-is-working-on-sorting