【问题标题】:GridView Sorting resets page to original dataGridView 排序将页面重置为原始数据
【发布时间】:2011-12-12 04:28:01
【问题描述】:

各位,我正在使用 VS2005 C# 和 SQL Server 2005。

我的 GridView 有一个搜索,我想使用 GridView 的 Sort 功能对我的搜索结果进行排序。

但是,每次我过滤结果后,当我按标题对我的 DataGrid 进行排序时,它总是将我的 GridView 重置为原始的完整数据桌子。

例如

a) 我的网页加载,一个包含 58 名学生数据的网格视图。

b) 我在 Names 下搜索 James 并且 GridView 中显示了 18 个结果。

c) 我按下标题 Class 以按类别对结果进行排序。

d) GridView 刷新并返回到 58 名学生的原始完整列表。


我试过了:

  1. 在单独的网页上实现搜索表,使其不会与原始网格视图发生冲突。

  2. 将网格视图名称更改为另一个名称。


我意识到:

当我将指针悬停在标题上方时,它将始终显示 javascript:_doPostBack('ctl00$MainContent$GridView1,Sort$Issue),

即使我可能已将 GridView1 更改为另一个名称


我需要一些解决方案来对 gridview 中的搜索结果进行排序,而不是在对它们进行排序时将原始数据带回。

【问题讨论】:

  • 你能不能明白这一点
  • 我无法得到我想要的结果,这就是我问的原因
  • 你有没有试过答案中给出的解决方案
  • 我试图在方法之外声明这两个变量,但无法声明。我应该在哪里申报?
  • 你需要在页面中同时声明为私有变量或私有属性......

标签: c# asp.net sql visual-studio-2005


【解决方案1】:

我已经实现了使用 ViewState 来存储排序方向,ASC 或 DESC,方式如下:

protected void GridView1_Sorting(object sender, GridViewSortEventArgs e)
    {
        listBindByName(); //this would be your procedure to look for the data you want
        DataSet dsSortTable = GridView1.DataSource as DataSet;
        DataTable dtSortTable = dsSortTable.Tables[0];
        if (dtSortTable != null)
        {
            DataView dvSortedView = new DataView(dtSortTable);
            dvSortedView.Sort = e.SortExpression + " " + getSortDirectionString();
            ViewState["sortExpression"] = e.SortExpression;
            GridView1.DataSource = dvSortedView;
            GridView1.DataBind();
        }
        UpdatePanel1.Update();
    }

    private string getSortDirectionString()
    {
        if (ViewState["sortDirection"] == null)
        {
            ViewState["sortDirection"] = "ASC";
        }
        else
        {
            if (ViewState["sortDirection"].ToString() == "ASC")
            {
                ViewState["sortDirection"] = "DESC";
                return ViewState["sortDirection"].ToString();
            }
            if (ViewState["sortDirection"].ToString() == "DESC")
            {
                ViewState["sortDirection"] = "ASC";
                return ViewState["sortDirection"].ToString();
            }
        }
        return ViewState["sortDirection"].ToString();
    }

如果有任何疑问,请告诉我。

【讨论】:

    【解决方案2】:

    假设在页面加载中调用gridview进行绑定

      protected void Page_Load(object sender, EventArgs e)
        {  
             fillgrid(false, string.Empty, false); //
        }
    
      public void fillgrid(bool sorting, string sortexpression, bool sortdir)
        {
              //binding codes 
               var data = from item in DBcontent.Tablename 
                          select new
                          {
                              Title = item.Title
                          }  
                 if (sorting)
                        {
                            if (sortexpression == "Title")
                            {
                                if (sortdir)
                                {
                                    GrieviewID.DataSource = data.OrderBy(id => id.Title).ToList();
                                }
                                else
                                {
                                    GrieviewID.DataSource = data.OrderByDescending(id => id.Title).ToList();
                                }
                            }
                       else
                        {
                            GrdID.DataSource = data.OrderByDescending(id => id.StartDate).ToList();
                        }
                    GrdID.DataBind();
        }
    
      protected void grdevents_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            grdevents.PageIndex = e.NewPageIndex;
            BindAndSortGrid();
        }
    
        /// <summary>
        /// Gets the sort direction.
        /// </summary>
        /// <param name="column">The column.</param>
        /// <returns></returns>
        private string GetSortDirection(string column)
        {
            // By default, set the sort direction to ascending.
            string sortDirection = "ASC";
    
            // Retrieve the last column that was sorted.
            string sortExpression = ViewState["SortExpression"] as string;
    
            if (sortExpression != null)
            {
                // Check if the same column is being sorted.
                // Otherwise, the default value can be returned.
                if (sortExpression == column)
                {
                    string lastDirection = ViewState["SortDirection"] as string;
                    if ((lastDirection != null) && (lastDirection == "ASC"))
                    {
                        sortDirection = "DESC";
                    }
                }
            }
    
            // Save new values in ViewState.
            ViewState["SortDirection"] = sortDirection;
            ViewState["SortExpression"] = column;
    
            return sortDirection;
        }
    
        /// <summary>
        /// Bind and sort grid.
        /// </summary>
        private void BindAndSortGrid()
        {
            bool sortdir;
            if (ViewState["SortDirection"] != null && ViewState["SortExpression"] != null)
            {
                sortdir = ViewState["SortDirection"].ToString() == "ASC" ? true : false;
                fillgrid(true, ViewState["SortExpression"].ToString(), sortdir);
            }
            else
                fillgrid(false, string.Empty, false);
        }
    
        protected void grdevents_Sorting(object sender, GridViewSortEventArgs e)
        {
            bool sortdir = GetSortDirection(e.SortExpression) == "ASC" ? true : false;
            fillgrid(true, e.SortExpression.ToString(), sortdir);
        }
    

    按照下面的代码...希望它的帮助

    【讨论】:

      猜你喜欢
      • 2021-01-02
      • 1970-01-01
      • 2019-08-02
      • 2014-05-28
      • 2012-01-16
      • 1970-01-01
      • 1970-01-01
      • 2023-03-14
      • 1970-01-01
      相关资源
      最近更新 更多