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;
}
}
{
/// <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;
}
}