65.9K
CodeProject 正在变化。 阅读更多。
Home

记录大量记录的 DataGrid 分页

starIconstarIcon
emptyStarIcon
starIcon
emptyStarIconemptyStarIcon

2.42/5 (10投票s)

2006年1月5日

1分钟阅读

viewsIcon

82000

优化 DataGrid 中的分页。

引言

许多人熟悉 ASP.NET DataGrid 中的分页。而且,你们中的大多数人可能知道,.NET Framework 中的标准分页方法不起作用(当然,我们不应该责怪这项技术)。因此,在本文中,我想向您介绍一种针对包含大量记录的 SQL 查询结果的快速分页方法。您可以在该网站上找到其他类似的文章,但我希望以更简洁的形式分享我的想法。请享受我这篇简短但非常有用的文章(正如我所假设的那样)。

使用代码

对于以下方法,您需要定义要从数据库中检索的表和列。您还需要确定要使用的表连接以及要提供的排序方式。那么,让我们开始吧。

此方法用于从查询中获取项目计数(对于分页是必需的,稍后会说明)。

public int GetRecordCount( string tableName )
{
    string sql = "select count(*) from "+ tableName;
    return Convert.ToInt32(executeScalar(sql));
}

此方法用于为数据库创建 SQL 查询

public string CreateSql(int pageNumber, int pageSize, string primaryKey, 
                        string fieldNames, string fromTables, 
                        string condition, string orderBy)
{
    string sql = " SELECT TOP "+pageSize+" "+ 
                 fieldNames+" FROM "+ fromTables +" ";
    if (condition!=null && !string.Empty.Equals(condition))
    {
        if (!condition.ToLower().Trim().StartsWith("where"))
        {
            sql += " where ";
        }
        sql += condition + " and ";
        
    }
    if (sql.ToLower().IndexOf("where")==-1)
        sql+= " where ";

    sql+= " " +primaryKey +" NOT IN " +
          " (SELECT TOP "+(pageNumber*pageSize)+ 
          " "+primaryKey+" FROM " + fromTables+" ";
    if (condition!=null && !string.Empty.Equals(condition))
    {
        if (!condition.ToLower().Trim().StartsWith("where"))
        {
            sql += " where ";
        }
        sql += condition;
    }

    if (orderBy!=null && !string.Empty.Equals(orderBy))
    {
        sql+= " ORDER BY "+orderBy+") "+
            " ORDER BY "+orderBy;
    }
    else
    {
        sql+=")";
    }
    return sql;
}

运行 SQL 查询

public void RunSql(string sqlString, 
            out SqlDataReader dataReader)
{
    SqlCommand cmd = CreateSqlCommand(sqlString,  null);
    dataReader = cmd.ExecuteReader();
}

提供了两个重载的查询创建方法。第一个用于已知查询中的总记录数(不是单页中的记录数),第二个用于其他情况。

public SqlDataReader GetListByPage (int pageNumber, int pageSize, 
                     string primaryKey, string fieldNames, 
                     string fromTables, string condition, string orderBy)
{
    string sql= CreateSql (pageNumber,pageSize, primaryKey, 
                fieldNames,fromTables,condition,orderBy);
    SqlDataReader dr;
    RunSql(sql, out dr);
    return dr;
}
public SqlDataReader GetListByPage (out int rowcount, int pageNumber, 
                     int pageSize, string primaryKey, string fieldNames, 
                     string fromTables, string condition, string orderBy)
{
    string countSql = fromTables;
    if (condition!=null && !string.Empty.Equals(condition))
    {

        if (!condition.ToLower().Trim().StartsWith("where"))
        {
            countSql += " where ";
        }
        countSql += condition;
    }
    rowcount = GetRecordCount(countSql);
    return GetListByPage(pageNumber, pageSize, primaryKey, 
                         fieldNames, fromTables, condition, orderBy);        
}

这里是一个使用此代码的小示例

myDataGrid.DataSource = db.GetListByPage(out rowcount, 
                        PageIndex,myDataGrid.PageSize, 
                        "KeyField",fieldNames,tables,
                        search,OrderBy);
myDataGrid.VirtualItemCount = rowcount;
myDataGrid.CurrentPageIndex = PageIndex;

别忘了将 AllowCustomPaging 属性设置为 true 在你的 DataGrid 中。目前就到这里吧。

特别感谢

我想特别感谢 Olexander Rudyy 在我上次项目中给予的巨大帮助!

© . All rights reserved.