C# 无法按des顺序对gridview进行排序,也无法对另一列进行排序?

C# 无法按des顺序对gridview进行排序,也无法对另一列进行排序?,c#,asp.net,sorting,gridview,C#,Asp.net,Sorting,Gridview,我有这个网格视图,它有两个问题 如果我通过单击一列对其排序,然后再次单击,则它不会按des顺序排序 如果有一次我用a列进行排序,然后单击任何其他列,那么它不会再次排序吗 <asp:GridView ID="grdReport" runat="server" AutoGenerateColumns="False" DataKeyNames="CustCode" ShowFooter="True" EmptyDataText="No record found" PageSize="50"

我有这个网格视图,它有两个问题

  • 如果我通过单击一列对其排序,然后再次单击,则它不会按des顺序排序
  • 如果有一次我用a列进行排序,然后单击任何其他列,那么它不会再次排序吗

      <asp:GridView ID="grdReport" runat="server" AutoGenerateColumns="False" DataKeyNames="CustCode"
    ShowFooter="True" EmptyDataText="No record found" PageSize="50"
    CssClass="mGrid" onrowdatabound="grdReport_RowDataBound"
    AllowSorting="True" onsorting="grdReport_Sorting">
    <Columns>
       <asp:TemplateField HeaderText="Select">
            <ItemTemplate>
                <asp:CheckBox ID="chkSelect" runat="server"/>
            </ItemTemplate>
       </asp:TemplateField>
    
       <asp:TemplateField Visible="false">
       <ItemTemplate>
       <asp:Label ID="lblCustCodes" runat="server" Text='<%# Eval("CustCode") %>' CssClass="grdCustName"></asp:Label>
       </ItemTemplate>
       </asp:TemplateField>
    
      <asp:TemplateField HeaderText="Customer" SortExpression="Customer">
      <ItemTemplate>
       <asp:HyperLink Target="_blank" Text='<%# Eval("CustomerName") %>' runat="server" ID="hplNavigate">
       </asp:HyperLink>
       </ItemTemplate>
       </asp:TemplateField>
        <asp:BoundField DataField="QTY" HeaderText="Booked Qty" HeaderStyle-HorizontalAlign="Right" ItemStyle-HorizontalAlign="Right"
            SortExpression="QTY">
            <FooterStyle HorizontalAlign="Right" />
            <ItemStyle HorizontalAlign="Right"></ItemStyle>
        </asp:BoundField>
           <asp:BoundField DataField="Volume" HeaderText="Booked Amt" HeaderStyle-HorizontalAlign="Right" ItemStyle-HorizontalAlign="Right"
            SortExpression="Volume">
            <FooterStyle HorizontalAlign="Right" />
            <ItemStyle HorizontalAlign="Right"></ItemStyle>
        </asp:BoundField>
        </asp:BoundField>
         <asp:BoundField DataField="FirstBill" HeaderText="First Bill" HeaderStyle-HorizontalAlign="left" ItemStyle-HorizontalAlign="left"
            SortExpression="FirstBill">
            <FooterStyle HorizontalAlign="Left" />
            <ItemStyle HorizontalAlign="Left"></ItemStyle>
        </asp:BoundField>
    </Columns>
    <FooterStyle BackColor="Aqua" Font-Bold="true" ForeColor="BlueViolet"/>
    
    再次列出问题

  • 再次单击时无法按des排序
  • 如果我按客户名称排序,然后单击
    qty
    或我得到的任何其他
    指定类型无效。
    意味着,一旦我按任何特定列排序,我就无法通过单击任何其他列进行排序
    有人能帮我解决这个问题吗?

    您必须允许gridview根据gridview属性进行排序

     AllowSorting="true" 
    
    此外,还必须将排序存储在缓存(viewstate或session)中。使用会话变量存储最新的排序表达式,并在下次对网格进行排序时,将网格的排序表达式与存储最后一个排序表达式的会话变量相比较。如果列相等,则检查上一次排序的方向和反向排序

    例如:

    DataTable sourceTable = GridAttendence.DataSource as DataTable;
    DataView view = new DataView(sourceTable);
    string[] sortData = Session["sortExpression"].ToString().Trim().Split(' ');
    if (e.SortExpression == sortData[0])
    {
        if (sortData[1] == "ASC")
        {
            view.Sort = e.SortExpression + " " + "DESC";
            this.ViewState["sortExpression"] = e.SortExpression + " " + "DESC";
        }
        else
        {
            view.Sort = e.SortExpression + " " + "ASC";
            this.ViewState["sortExpression"] = e.SortExpression + " " + "ASC";
        }
    }
    else
    {
        view.Sort = e.SortExpression + " " + "ASC";
        this.ViewState["sortExpression"] = e.SortExpression + " " + "ASC";
    }
    

    在网格中,当AutoGenerateColumns=“false”时,e.SortDirection将始终递增。解决方法是需要在ViewState中存储列的最后一个排序器(或存储每个列的最后一个排序顺序)

        public const string ASCENDING = "Ascending";
        public const string DESCENDING = "Descending";
    
    定义属性以保留排序器

        public string GridSortOrder
        {
            get { return Convert.ToString(ViewState["SortOrderKey"]) == string.Empty ? ASCENDING : "Descending"; }
            set { ViewState["SortOrderKey"] = value; }
        }
    
    在grdReport_排序事件中

         if (GridSortOrder == ASCENDING)
            {
                var result = from table in Ob.DataTableOther.AsEnumerable()
                             orderby table.Field<string>("CustomerName")
                             select table;
                var dv = result.AsDataView();
                grdReport.DataSource = dv;
                grdReport.DataBind();
    
            }
            else
            {
                var result = from table in Ob.DataTableOther.AsEnumerable()
                             orderby table.Field<string>("CustomerName") descending
                             select table;
                var dv = result.AsDataView();
                grdReport.DataSource = dv;
                grdReport.DataBind();   
            }
    
             /*
             * logic for remaining columns 
             */
    
            //Change the sortOrder
            ChangeSortOrder(GridSortOrder);
    
    试试这个方法

    private const string ASCENDING = " ASC";
    private const string DESCENDING = " DESC";
    static private DataView dvReports;
    
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            // i am assuming that you are binding the gridview on page_load
            // you can use the same on a button click event too
            dvReports = new DataView(Ob.DataTableOther);
            BindGridView();
        }
    }
    
    protected void grdReport_Sorting(object sender, GridViewSortEventArgs e)
    {
        string sortExpression = e.SortExpression;
    
        if (GridViewSortDirection == SortDirection.Ascending)
        {
            GridViewSortDirection = SortDirection.Descending;
            dvProducts.Sort = sortExpression + DESCENDING;
        }
        else
        {
            GridViewSortDirection = SortDirection.Ascending;
            dvProducts.Sort = sortExpression + ASCENDING;
        }
        BindGridView();
    }
    
    private void BindGridView()
    {
        GridView1.DataSource = dvReports
        GridView1.DataBind();
    }
    
    public SortDirection GridViewSortDirection
    {
        get
        {
            if (ViewState["sortDirection"] == null)
                ViewState["sortDirection"] = SortDirection.Ascending;
    
            return (SortDirection)ViewState["sortDirection"];
        }
        set { ViewState["sortDirection"] = value; }
    }
    
    我之所以为您写这篇文章,是因为如果您使用
    DataTable
    进行绑定,则无需使其
    IEnumerable
    并进行自定义排序。这是不必要的,而且还有更多的代码行


    框架是你的朋友。愉快的编码。

    您可能需要将
    会话[“sortExpression”]
    更改为
    ViewState[“sortExpression”]
         if (GridSortOrder == ASCENDING)
            {
                var result = from table in Ob.DataTableOther.AsEnumerable()
                             orderby table.Field<string>("CustomerName")
                             select table;
                var dv = result.AsDataView();
                grdReport.DataSource = dv;
                grdReport.DataBind();
    
            }
            else
            {
                var result = from table in Ob.DataTableOther.AsEnumerable()
                             orderby table.Field<string>("CustomerName") descending
                             select table;
                var dv = result.AsDataView();
                grdReport.DataSource = dv;
                grdReport.DataBind();   
            }
    
             /*
             * logic for remaining columns 
             */
    
            //Change the sortOrder
            ChangeSortOrder(GridSortOrder);
    
        public void ChangeSortOrder(string currentOrder)
        {
            GridSortOrder = currentOrder == ASCENDING ? DESCENDING : ASCENDING;
        }
    
    private const string ASCENDING = " ASC";
    private const string DESCENDING = " DESC";
    static private DataView dvReports;
    
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            // i am assuming that you are binding the gridview on page_load
            // you can use the same on a button click event too
            dvReports = new DataView(Ob.DataTableOther);
            BindGridView();
        }
    }
    
    protected void grdReport_Sorting(object sender, GridViewSortEventArgs e)
    {
        string sortExpression = e.SortExpression;
    
        if (GridViewSortDirection == SortDirection.Ascending)
        {
            GridViewSortDirection = SortDirection.Descending;
            dvProducts.Sort = sortExpression + DESCENDING;
        }
        else
        {
            GridViewSortDirection = SortDirection.Ascending;
            dvProducts.Sort = sortExpression + ASCENDING;
        }
        BindGridView();
    }
    
    private void BindGridView()
    {
        GridView1.DataSource = dvReports
        GridView1.DataBind();
    }
    
    public SortDirection GridViewSortDirection
    {
        get
        {
            if (ViewState["sortDirection"] == null)
                ViewState["sortDirection"] = SortDirection.Ascending;
    
            return (SortDirection)ViewState["sortDirection"];
        }
        set { ViewState["sortDirection"] = value; }
    }