问题:GridView/ObjectDataSource 的排序根据页面而变化

发布于 2024-08-01 19:31:18 字数 1970 浏览 4 评论 0原文

我有一个 GridView 使用分页绑定到 ObjectDataSource。 分页工作正常,只是排序顺序根据正在查看的结果页面而变化。 这会导致项目重新出现在后续页面上以及其他问题中。 我将问题追溯到我的 DAL,它一次读取一页,然后对其进行排序。 显然,排序会随着结果集大小的变化而变化。 这个算法有没有改进。 如果可能的话我想使用数据读取器:

    [System.ComponentModel.DataObjectMethod(System.ComponentModel.DataObjectMethodType.Select)]
    public static WordsCollection LoadForCriteria(string sqlCriteria, int maximumRows, int startRowIndex, string sortExpression)
    {
        //DEFAULT SORT EXPRESSION
        if (string.IsNullOrEmpty(sortExpression)) sortExpression = "OrderBy";
        //CREATE THE DYNAMIC SQL TO LOAD OBJECT
        StringBuilder selectQuery = new StringBuilder();
        selectQuery.Append("SELECT");
        if (maximumRows > 0) selectQuery.Append(" TOP " + (startRowIndex + maximumRows).ToString());
        selectQuery.Append(" " + Words.GetColumnNames(string.Empty));
        selectQuery.Append(" FROM sw_Words");
        string whereClause = string.IsNullOrEmpty(sqlCriteria) ? string.Empty : " WHERE " + sqlCriteria;
        selectQuery.Append(whereClause);
        selectQuery.Append(" ORDER BY " + sortExpression);
        Database database = Token.Instance.Database;
        DbCommand selectCommand = database.GetSqlStringCommand(selectQuery.ToString());
        //EXECUTE THE COMMAND
        WordsCollection results = new WordsCollection();
        int thisIndex = 0;
        int rowCount = 0;
        using (IDataReader dr = database.ExecuteReader(selectCommand))
        {
            while (dr.Read() && ((maximumRows < 1) || (rowCount < maximumRows)))
            {
                if (thisIndex >= startRowIndex)
                {
                    Words varWords = new Words();
                    Words.LoadDataReader(varWords, dr);
                    results.Add(varWords);
                    rowCount++;
                }
                thisIndex++;
            }
            dr.Close();
        }
        return results;
    }

I have a GridView tied to an ObjectDataSource using paging. The paging works fine, except that the sort order changes depending on which page of the results is being viewed. This causes items to reappear on subsequent pages among other issues. I traced the problem to my DAL, which reads a page at a time and then sorts it. Obviously the sorting is going to change as the result set size changes. Is there an improvement to this algorithm. I would like to use a datareader if possible:

    [System.ComponentModel.DataObjectMethod(System.ComponentModel.DataObjectMethodType.Select)]
    public static WordsCollection LoadForCriteria(string sqlCriteria, int maximumRows, int startRowIndex, string sortExpression)
    {
        //DEFAULT SORT EXPRESSION
        if (string.IsNullOrEmpty(sortExpression)) sortExpression = "OrderBy";
        //CREATE THE DYNAMIC SQL TO LOAD OBJECT
        StringBuilder selectQuery = new StringBuilder();
        selectQuery.Append("SELECT");
        if (maximumRows > 0) selectQuery.Append(" TOP " + (startRowIndex + maximumRows).ToString());
        selectQuery.Append(" " + Words.GetColumnNames(string.Empty));
        selectQuery.Append(" FROM sw_Words");
        string whereClause = string.IsNullOrEmpty(sqlCriteria) ? string.Empty : " WHERE " + sqlCriteria;
        selectQuery.Append(whereClause);
        selectQuery.Append(" ORDER BY " + sortExpression);
        Database database = Token.Instance.Database;
        DbCommand selectCommand = database.GetSqlStringCommand(selectQuery.ToString());
        //EXECUTE THE COMMAND
        WordsCollection results = new WordsCollection();
        int thisIndex = 0;
        int rowCount = 0;
        using (IDataReader dr = database.ExecuteReader(selectCommand))
        {
            while (dr.Read() && ((maximumRows < 1) || (rowCount < maximumRows)))
            {
                if (thisIndex >= startRowIndex)
                {
                    Words varWords = new Words();
                    Words.LoadDataReader(varWords, dr);
                    results.Add(varWords);
                    rowCount++;
                }
                thisIndex++;
            }
            dr.Close();
        }
        return results;
    }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

一曲爱恨情仇 2024-08-08 19:31:18

我通过阅读MSDN找到了解决这个问题的方法。 诀窍是执行完整查询并仅返回感兴趣的子集,这样排序始终保持一致。 但是,该过程仅适用于 SQL 2005。

    [System.ComponentModel.DataObjectMethod(System.ComponentModel.DataObjectMethodType.Select)]
    public static WordsCollection LoadForCriteria(string sqlCriteria, int maximumRows, int startRowIndex, string sortExpression)
    {
        //DEFAULT SORT EXPRESSION
        if (string.IsNullOrEmpty(sortExpression)) sortExpression = "OrderBy";
        //CREATE THE DYNAMIC SQL TO LOAD OBJECT
        StringBuilder selectQuery = new StringBuilder();
        selectQuery.Append("SELECT ");
        selectQuery.Append(Words.GetColumnNames(string.Empty));
        selectQuery.Append(" FROM (");
        selectQuery.Append("SELECT ");
        selectQuery.Append(Words.GetColumnNames(string.Empty));
        selectQuery.Append(", ROW_NUMBER() OVER (ORDER BY " + sortExpression + ") AS RowRank");
        selectQuery.Append(" FROM sw_Words) AS WordsWithRowNumbers");
        selectQuery.Append(" WHERE RowRank >" + startRowIndex.ToString() + " AND " + "RowRank <=" + (startRowIndex + maximumRows).ToString());
        string whereClause = string.IsNullOrEmpty(sqlCriteria) ? string.Empty : " AND " + sqlCriteria;
        selectQuery.Append(whereClause);
        Database database = Token.Instance.Database;
        DbCommand selectCommand = database.GetSqlStringCommand(selectQuery.ToString());
        //EXECUTE THE COMMAND
        WordsCollection results = new WordsCollection();
        int rowCount = 0;
        using (IDataReader dr = database.ExecuteReader(selectCommand))
        {
            while (dr.Read())
            {
                Words varWords = new Words();
                Words.LoadDataReader(varWords, dr);
                results.Add(varWords);
                rowCount++;
            }
            dr.Close();
        }
        return results;
    }

I found a solution to this problem from reading MSDN. The trick is to perform the full query and return only the interesting subset that way the sort is always consistent. The procedure,however, only works using SQL 2005.

    [System.ComponentModel.DataObjectMethod(System.ComponentModel.DataObjectMethodType.Select)]
    public static WordsCollection LoadForCriteria(string sqlCriteria, int maximumRows, int startRowIndex, string sortExpression)
    {
        //DEFAULT SORT EXPRESSION
        if (string.IsNullOrEmpty(sortExpression)) sortExpression = "OrderBy";
        //CREATE THE DYNAMIC SQL TO LOAD OBJECT
        StringBuilder selectQuery = new StringBuilder();
        selectQuery.Append("SELECT ");
        selectQuery.Append(Words.GetColumnNames(string.Empty));
        selectQuery.Append(" FROM (");
        selectQuery.Append("SELECT ");
        selectQuery.Append(Words.GetColumnNames(string.Empty));
        selectQuery.Append(", ROW_NUMBER() OVER (ORDER BY " + sortExpression + ") AS RowRank");
        selectQuery.Append(" FROM sw_Words) AS WordsWithRowNumbers");
        selectQuery.Append(" WHERE RowRank >" + startRowIndex.ToString() + " AND " + "RowRank <=" + (startRowIndex + maximumRows).ToString());
        string whereClause = string.IsNullOrEmpty(sqlCriteria) ? string.Empty : " AND " + sqlCriteria;
        selectQuery.Append(whereClause);
        Database database = Token.Instance.Database;
        DbCommand selectCommand = database.GetSqlStringCommand(selectQuery.ToString());
        //EXECUTE THE COMMAND
        WordsCollection results = new WordsCollection();
        int rowCount = 0;
        using (IDataReader dr = database.ExecuteReader(selectCommand))
        {
            while (dr.Read())
            {
                Words varWords = new Words();
                Words.LoadDataReader(varWords, dr);
                results.Add(varWords);
                rowCount++;
            }
            dr.Close();
        }
        return results;
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文