ExtendCls.cs 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. using Infragistics.Win;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Data;
  5. using System.Linq;
  6. using System.Reflection;
  7. using System.Text;
  8. using System.Threading.Tasks;
  9. using System.Windows.Forms;
  10. namespace Common
  11. {
  12. public static class ExtendCls
  13. {
  14. public static ValueList ListToValuelist<T>(this List<T> items){
  15. ValueList vlistScale = new ValueList();
  16. vlistScale.ValueListItems.Add("", "全部");
  17. vlistScale.ValueListItems.Add("0", "无效");
  18. vlistScale.ValueListItems.Add("1", "未使用");
  19. vlistScale.ValueListItems.Add("2", "使用中");
  20. vlistScale.ValueListItems.Add("3", "已完成");
  21. return vlistScale;
  22. }
  23. #region List转dataTable
  24. public static DataTable ListToDataTable<T>(this List<T> items)
  25. {
  26. DataTable dataTable = new DataTable();
  27. try
  28. {
  29. PropertyInfo[] Props = typeof(T).GetProperties(BindingFlags.Public | BindingFlags.Instance);
  30. foreach (PropertyInfo prop in Props)
  31. {
  32. dataTable.Columns.Add(prop.Name);
  33. }
  34. foreach (T obj in items)
  35. {
  36. var values = new object[Props.Length];
  37. for (int i = 0; i < Props.Length; i++)
  38. {
  39. values[i] = Props[i].GetValue(obj, null);
  40. }
  41. dataTable.Rows.Add(values);
  42. }
  43. return dataTable;
  44. }
  45. catch (Exception ex)
  46. {
  47. MessageBox.Show("公共方法ListToDataTable报错!错误内容:\r\n" + ex.Message);
  48. return dataTable;
  49. }
  50. }
  51. #endregion
  52. #region dataTable转List
  53. public static List<T> TableToDataList<T>(this DataTable table)
  54. {
  55. if (table == null)
  56. {
  57. return null;
  58. }
  59. List<DataRow> rows = new List<DataRow>();
  60. foreach (DataRow row in table.Rows)
  61. {
  62. rows.Add(row);
  63. }
  64. return ConvertTo<T>(rows);
  65. }
  66. private static List<T> ConvertTo<T>(List<DataRow> rows)
  67. {
  68. List<T> list = null;
  69. if (rows != null)
  70. {
  71. list = new List<T>();
  72. foreach (DataRow row in rows)
  73. {
  74. T item = row.CreateItem<T>();//CreateItem<T>(row);
  75. list.Add(item);
  76. }
  77. }
  78. return list;
  79. }
  80. public static T CreateItem<T>(this DataRow row)
  81. {
  82. T obj = default(T);
  83. try
  84. {
  85. if (row != null)
  86. {
  87. obj = Activator.CreateInstance<T>();
  88. foreach (DataColumn column in row.Table.Columns)
  89. {
  90. PropertyInfo prop = obj.GetType().GetProperty(column.ColumnName);
  91. object value = row[column.ColumnName];
  92. if (value.ToString() != "")
  93. {
  94. if (prop.PropertyType.FullName == typeof(string).FullName)
  95. {
  96. prop.SetValue(obj, value.ToString(), null);
  97. }
  98. else if (prop.PropertyType.FullName == typeof(DateTime?).FullName)
  99. {
  100. prop.SetValue(obj, (DateTime?)Convert.ToDateTime(value.ToString()), null);
  101. }
  102. else if (prop.PropertyType.FullName == typeof(int?).FullName)
  103. {
  104. prop.SetValue(obj, (int?)Convert.ToInt32(value.ToString()), null);
  105. }
  106. else if (prop.PropertyType.FullName == typeof(long?).FullName)
  107. {
  108. prop.SetValue(obj, (long?)Convert.ToInt32(value.ToString()), null);
  109. }
  110. else if (prop.PropertyType.FullName == typeof(double?).FullName)
  111. {
  112. prop.SetValue(obj, (double?)Convert.ToDouble(value.ToString()), null);
  113. }
  114. else if (prop.PropertyType.FullName == typeof(bool).FullName)
  115. {
  116. prop.SetValue(obj, Convert.ToBoolean(value.ToString()), null);
  117. }
  118. else if (prop.PropertyType.FullName == typeof(decimal?).FullName)
  119. {
  120. prop.SetValue(obj, Convert.ToDecimal(value.ToString()), null);
  121. }
  122. //prop.SetValue(obj, value, null);
  123. }
  124. }
  125. }
  126. }
  127. catch (Exception ex)
  128. { //You can log something here
  129. //throw;
  130. }
  131. return obj;
  132. }
  133. #endregion
  134. }
  135. }