GridView控件只ASC正在整理 [英] gridview only ASC is working on sorting

查看:120
本文介绍了GridView控件只ASC正在整理的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我整理工作,但它只能运行升序。当我再次点击它只是运行升序还是。我调试它和sortDirection不会改变。
这里是我的排序code。我看着使用ViewState的,但无法找到使用它的这种局面的清洁方式。

 私人字符串ConvertSortDirectionToSql(SortDirection sortDirection)
{
    字符串newSortDirection =的String.Empty;    开关(sortDirection)
    {
        案例SortDirection.Ascending:
            newSortDirection =ASC;
            打破;        案例SortDirection.Descending:
            newSortDirection =DESC;
            打破;
    }    返回newSortDirection;
}保护无效caseloads_Sorting(对象发件人,GridViewSortEventArgs E)
{
    DataTable中的dataTable = caseloads.DataSource的数据表;    如果(dataTable的!= NULL)
    {
        数据视图数据视图=新的数据视图(dataTable的);
        dataView.Sort = e.SortEx pression ++ ConvertSortDirectionToSql(e.SortDirection);        caseloads.DataSource =数据视图;
        caseloads.DataBind();
    }
}


解决方案

于是我就用视图状态。它集开始递增,所以你甚至可以将它更改为,如果你想这样做降序。该案件数量();是我的gridview的存储和拉动的所有信息。

 保护无效的Page_Load(对象发件人,EventArgs的发送)
{
    如果(!的IsPostBack)
    {
        的ViewState [排序] =升序;
    }
        案件数量();
}

然后在排序,我们有如果正在寻找升序和降序。

语句

 私人字符串ConvertSortDirectionToSql(SortDirection sortDirection)
{
    字符串newSortDirection =的String.Empty;
    串排序= Convert.ToString(的ViewState [排序]);    如果(排序==升序)
    {
        newSortDirection =ASC;
        的ViewState [排序] =降序;
    }
    否则,如果(排序==降序)
    {
        newSortDirection =DESC;
        的ViewState [排序] =升序;
    }    返回newSortDirection;
}

下面是在GridView排序

 保护无效caseloads_Sorting(对象发件人,GridViewSortEventArgs E)
{
    DataTable中的dataTable = caseloads.DataSource的数据表;    如果(dataTable的!= NULL)
    {
        数据视图数据视图=新的数据视图(dataTable的);
        dataTable的= dataView.ToTable();
        dataView.Sort = e.SortEx pression ++ ConvertSortDirectionToSql(e.SortDirection);    caseloads.DataSource =数据视图;
    caseloads.DataBind();
    }
}

下面是我使用GridView控件在ASP

 < ASP:GridView控件ID =案件数量=服务器的AutoGenerateColumns =false的网格线=无
                                FONT-SIZE =12.5px背景色=#FFFFFF的CssClass =MGRIDOnSorting =caseloads_SortingAllowSorting =真>
                                <柱体和GT;
                                    < ASP:的TemplateField的HeaderText =客户端IDSORTEX pression =ClientKey>
                                        <&ItemTemplate中GT;
                                            < ASP:标签ID =clientKey1=服务器文本='<%#的eval(ClientKey)%>' />
                                        < / ItemTemplate中>
                                    < / ASP:的TemplateField>
                                    < ASP:的TemplateField的HeaderText =全名SORTEX pression =ConsumerName>
                                        <&ItemTemplate中GT;
                                            < ASP:标签ID =clientKey2=服务器文本='<%#的eval(ConsumerName)%>' />
                                        < / ItemTemplate中>
                                    < / ASP:的TemplateField>
                                    < ASP:的TemplateField的HeaderText =程序SORTEX pression =信息>
                                        <&ItemTemplate中GT;
                                            < ASP:标签ID =clientKey3=服务器文本='<%#的eval(信息)%GT;' />
                                        < / ItemTemplate中>
                                    < / ASP:的TemplateField>
                                    < ASP:的TemplateField的HeaderText =豪斯医生SORTEX pression =phoneH>
                                        <&ItemTemplate中GT;
                                            < ASP:标签ID =clientKey4=服务器文本='<%#的eval(phoneH)%>' />
                                        < / ItemTemplate中>
                                    < / ASP:的TemplateField>
                                    < ASP:的TemplateField的HeaderText =细胞SORTEX pression =phoneC>
                                        <&ItemTemplate中GT;
                                            < ASP:标签ID =clientKey5=服务器文本='<%#的eval(phoneC)%>' />
                                        < / ItemTemplate中>
                                    < / ASP:的TemplateField>
                                < /专栏>
                            < / ASP:GridView的>

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();
    }
}

解决方案

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>

这篇关于GridView控件只ASC正在整理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆