DataRowCollectionExtension.cs 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Data;
  6. namespace Bowin.Common.Utility
  7. {
  8. /// <summary>
  9. /// DataRowCollection 的扩展
  10. /// </summary>
  11. public static class DataRowCollectionExtension
  12. {
  13. /// <summary>
  14. /// 移除所有空行。
  15. /// </summary>
  16. /// <param name="dataRowCollection"></param>
  17. public static void RemoveAllEmpty(this DataRowCollection dataRowCollection)
  18. {
  19. if (dataRowCollection == null)
  20. {
  21. throw new ArgumentNullException("dataRowCollection");
  22. }
  23. List<DataRow> emptyRows = new List<DataRow>();
  24. foreach (DataRow dr in dataRowCollection)
  25. {
  26. var temp = 0;
  27. for (int i = 0; i < dr.Table.Columns.Count; i++)
  28. {
  29. if (dr[i] == null || string.IsNullOrWhiteSpace(dr[i].ToString()))
  30. {
  31. temp += 1;
  32. }
  33. }
  34. if (temp == dr.Table.Columns.Count)
  35. {
  36. emptyRows.Add(dr);
  37. }
  38. }
  39. dataRowCollection.RemoveRange(emptyRows);
  40. }
  41. /// <summary>
  42. /// 将指定的 System.Data.DataRow集合 添加到 System.Data.DataRowCollection 对象中。
  43. /// </summary>
  44. /// <param name="dataRowCollection"></param>
  45. /// <param name="collection"></param>
  46. public static void AddRange(this DataRowCollection dataRowCollection, IEnumerable<DataRow> collection)
  47. {
  48. if (dataRowCollection == null)
  49. {
  50. throw new ArgumentNullException("dataRowCollection");
  51. }
  52. if (collection == null)
  53. {
  54. throw new ArgumentNullException("collection");
  55. }
  56. foreach (DataRow dr in collection)
  57. {
  58. dataRowCollection.Add(dr);
  59. }
  60. }
  61. /// <summary>
  62. /// 从集合中移除指定的 System.Data.DataRow 集合。
  63. /// </summary>
  64. /// <param name="dataRowCollection"></param>
  65. /// <param name="collection"></param>
  66. public static void RemoveRange(this DataRowCollection dataRowCollection, IEnumerable<DataRow> collection)
  67. {
  68. if (dataRowCollection == null)
  69. {
  70. throw new ArgumentNullException("dataRowCollection");
  71. }
  72. if (collection == null)
  73. {
  74. throw new ArgumentNullException("collection");
  75. }
  76. foreach (DataRow dr in collection)
  77. {
  78. dataRowCollection.Remove(dr);
  79. }
  80. }
  81. }
  82. }