爱思开
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

62 lines
2.1 KiB

2 years ago
  1. using System.Data;
  2. namespace NFine.Code
  3. {
  4. public static class ExtTable
  5. {
  6. /// <summary>
  7. /// 获取表里某页的数据
  8. /// </summary>
  9. /// <param name="data">表数据</param>
  10. /// <param name="pageIndex">当前页</param>
  11. /// <param name="pageSize">分页大小</param>
  12. /// <param name="allPage">返回总页数</param>
  13. /// <returns>返回当页表数据</returns>
  14. public static DataTable GetPage(this DataTable data, int pageIndex, int pageSize, out int allPage)
  15. {
  16. allPage = data.Rows.Count / pageSize;
  17. allPage += data.Rows.Count % pageSize == 0 ? 0 : 1;
  18. DataTable Ntable = data.Clone();
  19. int startIndex = pageIndex * pageSize;
  20. int endIndex = startIndex + pageSize > data.Rows.Count ? data.Rows.Count : startIndex + pageSize;
  21. if (startIndex < endIndex)
  22. for (int i = startIndex; i < endIndex; i++)
  23. {
  24. Ntable.ImportRow(data.Rows[i]);
  25. }
  26. return Ntable;
  27. }
  28. /// <summary>
  29. /// 根据字段过滤表的内容
  30. /// </summary>
  31. /// <param name="data">表数据</param>
  32. /// <param name="condition">条件</param>
  33. /// <returns></returns>
  34. ///
  35. public static DataTable GetDataFilter(DataTable data, string condition)
  36. {
  37. if (data != null && data.Rows.Count > 0)
  38. {
  39. if (condition.Trim() == "")
  40. {
  41. return data;
  42. }
  43. else
  44. {
  45. DataTable newdt = new DataTable();
  46. newdt = data.Clone();
  47. DataRow[] dr = data.Select(condition);
  48. for (int i = 0; i < dr.Length; i++)
  49. {
  50. newdt.ImportRow((DataRow)dr[i]);
  51. }
  52. return newdt;
  53. }
  54. }
  55. else
  56. {
  57. return null;
  58. }
  59. }
  60. }
  61. }