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

时间:2013-08-13 16:00:16

标签: c# sorting gridview ascx

我正在使用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();
    }
}

2 个答案:

答案 0 :(得分:0)

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

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

另外,要小心你在ViewState中放置的内容,放置sql查询不是好习惯。

答案 1 :(得分:0)

这种方法对我有用。

您可以设置几个变量:

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.Sort。