gridview only ASC is working on sorting

点点圈 提交于 2020-01-06 03:01:08

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!