所以我有排序工作,但它只运行Ascending。当我再次点击它时,只运行Ascending。我调试了它,sortDirection永远不会改变。 这是我的排序代码。我调查了使用viewstate,但在这种情况下无法找到一种干净的方式。
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();
}
}
答案 0 :(得分:-1)
所以我选择了状态。它将开始设置为Ascending,因此如果您希望这样做,您甚至可以将其更改为Descending。 caseLoads();是我的gridview存储的地方,并提取所有信息。
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
ViewState["sorting"] = "Ascending";
}
caseLoads();
}
然后在排序中我们有if语句正在寻找Ascending和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;
}
这是gridview排序
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();
}
}
这是我在asp中使用的gridview
<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>