GridViewHelper.cs 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Web.UI;
  4. using System.Web.UI.WebControls;
  5. using System.Data;
  6. using System.Collections;
  7. using System.Reflection;
  8. namespace Ant.Service.Utilities
  9. {
  10. //public class GridViewHelper
  11. //{
  12. // #region 私有方法
  13. // /// <summary>
  14. // /// 截取内容长度
  15. // /// </summary>
  16. // /// <param name="o_Str">原字符串</param>
  17. // /// <param name="len">截取长度</param>
  18. // /// <returns>截取后字符串</returns>
  19. // private static string GetStrPartly(string o_Str, int len)
  20. // {
  21. // if (len == 0)
  22. // {
  23. // return o_Str;
  24. // }
  25. // else
  26. // {
  27. // if (o_Str.Length > len)
  28. // {
  29. // return o_Str.Substring(0, len) + "..";
  30. // }
  31. // else
  32. // {
  33. // return o_Str;
  34. // }
  35. // }
  36. // }
  37. // /// <summary>
  38. // /// 获取单元格内容
  39. // /// </summary>
  40. // /// <param name="cell">TableCell</param>
  41. // /// <returns>内容</returns>
  42. // private static string GetCellText(TableCell cell)
  43. // {
  44. // string text = cell.Text;
  45. // if (!string.IsNullOrEmpty(text))
  46. // {
  47. // return text;
  48. // }
  49. // foreach (System.Web.UI.Control control in cell.Controls)
  50. // {
  51. // if (control != null && control is IButtonControl)
  52. // {
  53. // IButtonControl btn = control as IButtonControl;
  54. // text = btn.Text.Replace("\r\n", "").Trim();
  55. // break;
  56. // }
  57. // if (control != null && control is ITextControl)
  58. // {
  59. // LiteralControl lc = control as LiteralControl;
  60. // if (lc != null)
  61. // {
  62. // continue;
  63. // }
  64. // ITextControl l = control as ITextControl;
  65. // text = l.Text.Replace("\r\n", "").Trim();
  66. // break;
  67. // }
  68. // }
  69. // return text;
  70. // }
  71. // /// <summary>
  72. // /// 设置单元格内容
  73. // /// </summary>
  74. // /// <param name="cell">TableCell</param>
  75. // /// <param name="maxLen">最大长度</param>
  76. // private static void SetCellText(TableCell cell, int maxLen)
  77. // {
  78. // string text = cell.Text;
  79. // if (!string.IsNullOrEmpty(text))
  80. // {
  81. // cell.Text = GetStrPartly(text, maxLen);
  82. // }
  83. // foreach (System.Web.UI.Control control in cell.Controls)
  84. // {
  85. // if (control != null && control is IButtonControl)
  86. // {
  87. // IButtonControl btn = control as IButtonControl;
  88. // text = btn.Text.Replace("\r\n", "").Trim();
  89. // btn.Text = GetStrPartly(text, maxLen);
  90. // break;
  91. // }
  92. // if (control != null && control is ITextControl)
  93. // {
  94. // LiteralControl lc = control as LiteralControl;
  95. // if (lc != null)
  96. // {
  97. // continue;
  98. // }
  99. // ITextControl l = control as ITextControl;
  100. // text = l.Text.Replace("\r\n", "").Trim();
  101. // if (l is DataBoundLiteralControl)
  102. // {
  103. // cell.Text = GetStrPartly(text, maxLen);
  104. // break;
  105. // }
  106. // else
  107. // {
  108. // l.Text = GetStrPartly(text, maxLen);
  109. // break;
  110. // }
  111. // }
  112. // }
  113. // }
  114. // #endregion
  115. // #region 公有方法
  116. // /// <summary>
  117. // /// 从GridView的数据生成DataTable
  118. // /// </summary>
  119. // /// <param name="gv">GridView对象</param>
  120. // public static DataTable GridView2DataTable(GridView gv)
  121. // {
  122. // DataTable table = new DataTable();
  123. // int rowIndex = 0;
  124. // List<string> cols = new List<string>();
  125. // if (!gv.ShowHeader && gv.Columns.Count == 0)
  126. // {
  127. // return table;
  128. // }
  129. // GridViewRow headerRow = gv.HeaderRow;
  130. // int columnCount = headerRow.Cells.Count;
  131. // for (int i = 0; i < columnCount; i++)
  132. // {
  133. // string text = GetCellText(headerRow.Cells[i]);
  134. // cols.Add(text);
  135. // }
  136. // foreach (GridViewRow r in gv.Rows)
  137. // {
  138. // if (r.RowType == DataControlRowType.DataRow)
  139. // {
  140. // DataRow row = table.NewRow();
  141. // int j = 0;
  142. // for (int i = 0; i < columnCount; i++)
  143. // {
  144. // string text = GetCellText(r.Cells[i]);
  145. // if (!String.IsNullOrEmpty(text))
  146. // {
  147. // if (rowIndex == 0)
  148. // {
  149. // string columnName = cols[i];
  150. // if (String.IsNullOrEmpty(columnName))
  151. // {
  152. // continue;
  153. // }
  154. // if (table.Columns.Contains(columnName))
  155. // {
  156. // continue;
  157. // }
  158. // DataColumn dc = table.Columns.Add();
  159. // dc.ColumnName = columnName;
  160. // dc.DataType = typeof(string);
  161. // }
  162. // row[j] = text;
  163. // j++;
  164. // }
  165. // }
  166. // rowIndex++;
  167. // table.Rows.Add(row);
  168. // }
  169. // }
  170. // return table;
  171. // }
  172. // /// <summary>
  173. // /// 将集合类转换成DataTable
  174. // /// </summary>
  175. // /// <param name="list">集合</param>
  176. // public static DataTable ToDataTable(IList list)
  177. // {
  178. // DataTable result = new DataTable();
  179. // if (list.Count > 0)
  180. // {
  181. // PropertyInfo[] propertys = list[0].GetType().GetProperties();
  182. // foreach (PropertyInfo pi in propertys)
  183. // {
  184. // result.Columns.Add(pi.Name, pi.PropertyType);
  185. // }
  186. // for (int i = 0; i < list.Count; i++)
  187. // {
  188. // ArrayList tempList = new ArrayList();
  189. // foreach (PropertyInfo pi in propertys)
  190. // {
  191. // object obj = pi.GetValue(list[i], null);
  192. // tempList.Add(obj);
  193. // }
  194. // object[] array = tempList.ToArray();
  195. // result.LoadDataRow(array, true);
  196. // }
  197. // }
  198. // return result;
  199. // }
  200. // /// <summary>
  201. // /// 将泛型集合类转换成DataTable
  202. // /// </summary>
  203. // /// <typeparam name="T">集合项类型</typeparam>
  204. // /// <param name="list">集合</param>
  205. // /// <param name="propertyName">需要返回的列的列名</param>
  206. // /// <returns>数据集(表)</returns>
  207. // public static DataTable ToDataTable<T>(IList<T> list, params string[] propertyName)
  208. // {
  209. // List<string> propertyNameList = new List<string>();
  210. // if (propertyName != null) propertyNameList.AddRange(propertyName);
  211. // DataTable result = new DataTable();
  212. // if (list.Count > 0)
  213. // {
  214. // PropertyInfo[] propertys = list[0].GetType().GetProperties();
  215. // foreach (PropertyInfo pi in propertys)
  216. // {
  217. // if (propertyNameList.Count == 0)
  218. // {
  219. // result.Columns.Add(pi.Name, pi.PropertyType);
  220. // }
  221. // else
  222. // {
  223. // if (propertyNameList.Contains(pi.Name)) result.Columns.Add(pi.Name, pi.PropertyType);
  224. // }
  225. // }
  226. // for (int i = 0; i < list.Count; i++)
  227. // {
  228. // ArrayList tempList = new ArrayList();
  229. // foreach (PropertyInfo pi in propertys)
  230. // {
  231. // if (propertyNameList.Count == 0)
  232. // {
  233. // object obj = pi.GetValue(list[i], null);
  234. // tempList.Add(obj);
  235. // }
  236. // else
  237. // {
  238. // if (propertyNameList.Contains(pi.Name))
  239. // {
  240. // object obj = pi.GetValue(list[i], null);
  241. // tempList.Add(obj);
  242. // }
  243. // }
  244. // }
  245. // object[] array = tempList.ToArray();
  246. // result.LoadDataRow(array, true);
  247. // }
  248. // }
  249. // return result;
  250. // }
  251. // #endregion
  252. //}
  253. }