• DataTable 中的查询、排序及分页(c#)


    public class GetDataTable
            {
                /// <summary>
                /// 对sourceDt进行排序,并且可以自由设置显示的条数
                /// --select top (num) from sourceDt order by strSort
                /// </summary>
                /// <param name="sourceDt">原DataTable</param>
                /// <param name="strSort">排序的字段,不用加order by (如: name desc 或 name desc,datecreated desc)</param>
                /// <param name="topNum">显示的数量(为0 是显示全部)</param>
                /// <returns>new DataTable</returns>
                public static DataTable SortTop(DataTable sourceDt, string strSort, int topNum)
                {
                    DataTable dt = sourceDt.Clone();

                    int totalCount = sourceDt.Rows.Count;
                    if (totalCount > 0)
                    {
                        DataView dv = sourceDt.DefaultView;
                        dv.Sort = strSort;

                        //计算要显示的条数
                        int count = topNum > 0 ? (topNum > totalCount ? totalCount : topNum) : totalCount;
                        for (int i = 0; i < count; i++)
                        {
                            dt.ImportRow(dv[i].Row);
                        }
                    }
                    return dt;
                }

                /// <summary>
                /// 对sourceDt进行查询和排序,并且可以自由设置显示的条数
                /// --select top (count) from sourceDt where strSelect order by strSort
                /// </summary>
                /// <param name="sourceDt">原DataTable</param>
                /// <param name="strSelect">查询条件(如:name='me')</param>
                /// <param name="strSort">排序字段,不用加order by (如: name desc 或 name desc,datecreated desc)</param>
                /// <param name="count">显示的数量(为0 是显示全部)</param>
                /// <returns>new DataTable</returns>
                public static DataTable SelectTop(DataTable sourceDt, string strSelect, string strSort, int count)
                {
                    DataTable dt = sourceDt.Clone();
                    bool noSort = string.IsNullOrEmpty(strSort);
                    DataRow[] dr = noSort ? sourceDt.Select(strSelect) : sourceDt.Select(strSelect, strSort);
                    foreach (DataRow row in dr)
                    {
                        dt.Rows.Add(row.ItemArray);
                    }

                    int dtCount = dt.Rows.Count;
                    if (dtCount > 0)
                    {
                        //计算要显示的条数
                        int num = count > 0 ? (count > dtCount ? dtCount : count) : dtCount;
                        DataView dv = dt.DefaultView;

                        if (!noSort)
                        {
                            dv.Sort = strSort;
                        }

                        for (int i = 0; i < num; i++)
                        {
                            dt.ImportRow(dv[i].Row);
                        }
                    }
                    return dt;
                }

                /// <summary>
                /// DateTable的分页操作
                /// </summary>
                /// <param name="dt">要进行分页的DataTable</param>
                /// <param name="currentPageIndex">当前页数</param>
                /// <param name="pageSize">一页显示的条数</param>
                /// <returns>第pageIndex页的数据</returns>
                public static DataTable SetPage(DataTable dt, int currentPageIndex, int pageSize)
                {
                    if (currentPageIndex == 0)
                    {
                        return dt;
                    }

                    DataTable newdt = dt.Clone();

                    int rowbegin = (currentPageIndex - 1) * pageSize;//当前页的第一条数据在dt中的位置
                    int rowend = currentPageIndex * pageSize;//当前页的最后一条数据在dt中的位置

                    if (rowbegin >= dt.Rows.Count)
                    {
                        return newdt;
                    }

                    if (rowend > dt.Rows.Count)
                    {
                        rowend = dt.Rows.Count;
                    }

                    DataView dv = dt.DefaultView;
                    for (int i = rowbegin; i <= rowend - 1; i++)
                    {
                        newdt.ImportRow(dv[i].Row);
                    }

                    return newdt;
                }

                /// <summary>
                /// 返回分页的页数
                /// </summary>
                /// <param name="count">总条数</param>
                /// <param name="pageye">每页显示多少条</param>
                /// <returns>如果 结尾为0:则返回1</returns>
                public static int PageCount(int count, int pageye)
                {
                    int page = 0;
                    int sesepage = pageye;
                    if (count % sesepage == 0) { page = count / sesepage; }
                    else { page = (count / sesepage) + 1; }
                    if (page == 0) { page += 1; }
                    return page;
                }
            }
  • 相关阅读:
    Linux三种网络连接模式
    hadoop知识点总结
    Wordpress 删除 Storefront 主题的购物车
    英文俚语600句及释义
    雅思口语俚语150句 A-Z of English Idioms: 150 Most Common Expressions
    雅思作文策略总结
    雅思作文高分词汇及词组
    剑桥雅思写作高分范文ESSAY113
    剑桥雅思写作高分范文ESSAY112
    剑桥雅思写作高分范文ESSAY111
  • 原文地址:https://www.cnblogs.com/qzbnet/p/2432296.html
Copyright © 2020-2023  润新知