在gridView中分页时如何保持排序顺序

本文关键字:何保持 排序 顺序 gridView 分页 | 更新日期: 2023-09-27 18:27:35

我使用gridView显示信息。除了一个细节外,它对我来说绝对完美。如果用户对一列进行排序,然后单击转到另一页,则排序将丢失。基本上,我需要在分页发生时保持排序,任何帮助都将不胜感激。这是我的代码:

public partial class DynamicGridView : System.Web.UI.UserControl
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (_selectCmd != string.Empty)
            BindData();
    }
    private void BindData()
    {
        DataTable dt;
        if (ViewState[gridView.ClientID + "datatable"] == null || (string)ViewState[gridView.ClientID + "selectCommand"] != _selectCmd)
        {
            ViewState[gridView.ClientID + "selectCommand"] = _selectCmd;
            dt = Classes.SQL.GetDataTable(_selectCmd);
            if (dt != null)
            {
                ViewState[gridView.ClientID + "datatable"] = dt;
                if (dt.Columns.Count != gridView.Columns.Count)
                {
                    gridView.Columns.Clear();
                    foreach (DataColumn column in dt.Columns)
                        AddBoundField(column);
                }
            }
        }
        else
            dt = (DataTable)ViewState[gridView.ClientID + "datatable"];
        gridView.DataSource = dt;
        gridView.DataBind();
    }
    private void AddBoundField(DataColumn column)
    {
        BoundField field = new BoundField();
        HandleCustomDataFormatting(ref field, column);
        field.DataField = column.ColumnName;
        field.HeaderText = column.ColumnName;
        field.ReadOnly = true;
        field.SortExpression = column.ColumnName;
        field.ControlStyle.ForeColor = System.Drawing.ColorTranslator.FromHtml("#333333");
        field.ItemStyle.Width = System.Web.UI.WebControls.Unit.Pixel(70);
        gridView.Columns.Add(field);
    }
    private void HandleCustomDataFormatting(ref BoundField field, DataColumn column)
    {
        if (column.DataType == typeof(System.DateTime))
            switch (column.ColumnName)
            {
                case "InvalidDate":
                case "NewCustomerValidated":
                case "ValidatedDate":
                    field.DataFormatString = "{0:d}";//exclude time info
                    break;
                default:
                    field.DataFormatString = "{0:g}";
                    break;
            }
    }
    private string GetSortDirection(string sortColumn)
    {
        string sortDirection = "ASC";
        string lastSortColumn = (string)ViewState[gridView.ClientID + "SortExpression"];
        if (lastSortColumn != null && lastSortColumn == sortColumn)
        {
            string lastDirection = (string)ViewState[gridView.ClientID + "SortDirection"];
            if (lastDirection != null && lastDirection == "ASC")
                sortDirection = "DESC";
        }
        ViewState[gridView.ClientID + "SortExpression"] = sortColumn;
        ViewState[gridView.ClientID + "SortDirection"] = sortDirection;
        return sortDirection;
    }
    private string _selectCmd = string.Empty;
    public string SelectCommand
    {
        set { _selectCmd = value; }
    }
    protected void GridView_Sorting(object sender, GridViewSortEventArgs e)
    {
        DataTable dt = (DataTable)ViewState[gridView.ClientID + "datatable"];
        dt.DefaultView.Sort = e.SortExpression + " " + GetSortDirection(e.SortExpression);
        BindData();
    }
    protected void GridView_PageChange(object sender, GridViewPageEventArgs e)
    {
        gridView.PageIndex = e.NewPageIndex;
        BindData();
    }
}

在gridView中分页时如何保持排序顺序

您不需要在每次页面加载时绑定。

if (_selectCmd != string.Empty)
    if(!IsPostBack)
            BindData();

此外,要小心在ViewState中放入的内容,放入sql查询不是一个好的做法。

这种方法对我很有效。

你可以设置几个变量如下:

protected string SortColummn 
{
    get { return ViewState["SortColumn"].ToString(); }
    set { ViewState["SortColumn"] = value; }
}
protected string SortDirection 
{
    get { return ViewState["SortDirection"].ToString(); }
    set { ViewState["SortDirection"] = value; }
}

在"if(!Page.IsPostBack)"块的Page_Load中为这些设置默认值。然后,在GridView_Sorting中,根据e.SortExpression设置这两个变量。最后,在BindData()中,设置dt。DefaultView.根据这两个变量进行排序。