PnCostBaseClass.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  1. using System;
  2. using Core.Mes.Client.Comm.Server;
  3. using Infragistics.Win.UltraWinEditors;
  4. using CoreFS.CA06;
  5. using System.Data;
  6. using Infragistics.Win.UltraWinGrid;
  7. using System.Drawing;
  8. using Infragistics.Win;
  9. using System.Text.RegularExpressions;
  10. using Core.Mes.Client.Comm.Control;
  11. using System.Collections.Generic;
  12. using System.ComponentModel;
  13. using System.Windows.Forms;
  14. namespace Core.StlMes.Client.PnCost
  15. {
  16. class PnCostBaseClass
  17. {
  18. /// <summary>
  19. /// 初始化下拉框(增强型)
  20. /// </summary>
  21. /// <param name="uce">下拉框</param>
  22. /// <param name="methodId">请求的服务</param>
  23. /// <param name="valueMember">值成员</param>
  24. /// <param name="textMember">显示成员</param>
  25. /// <param name="ob">ob对象</param>
  26. /// <param name="isEmpty">是否添加空行</param>
  27. public static void InitComboEditorNew(UltraComboEditor uce, string methodId, string valueMember, string textMember, OpeBase ob, bool isEmpty)
  28. {
  29. InitComboEditor(uce, methodId, valueMember, ob, isEmpty);
  30. uce.DisplayMember = textMember;
  31. }
  32. /// <summary>
  33. /// 初始化下拉框
  34. /// </summary>
  35. /// <param name="uce">下拉框</param>
  36. /// <param name="methodId">请求的服务</param>
  37. /// <param name="valueMember">值成员</param>
  38. /// <param name="ob">ob对象</param>
  39. /// <param name="isEmpty">是否有空行</param>
  40. public static void InitComboEditor(UltraComboEditor uce, string methodId, string valueMember, OpeBase ob, bool isEmpty)
  41. {
  42. DataTable dt = ServerHelper.GetData(methodId, null, ob);
  43. if (dt != null && dt.Rows.Count > 0)
  44. {
  45. if (isEmpty)
  46. {
  47. Object[] obj = new Object[] { "", "" };
  48. DataRow dr = dt.NewRow();
  49. dr.ItemArray = obj;
  50. dt.Rows.InsertAt(dr, 0);
  51. }
  52. uce.DataSource = dt;
  53. uce.ValueMember = valueMember;
  54. SetComboItemHeight(uce);
  55. }
  56. }
  57. /// <summary>
  58. /// 初始化下拉框 --带参数的
  59. /// </summary>
  60. /// <param name="uce"></param>
  61. /// <param name="methodId"></param>
  62. /// <param name="valueMember"></param>
  63. /// <param name="ob"></param>
  64. /// <param name="isEmpty"></param>
  65. /// <param name="obj"></param>
  66. public static void InitComboEditorWithParm(UltraComboEditor uce, string methodId, string valueMember, OpeBase ob, bool isEmpty, Object[] parm)
  67. {
  68. DataTable dt = ServerHelper.GetData(methodId, parm, ob);
  69. if (dt != null && dt.Rows.Count > 0)
  70. {
  71. if (isEmpty)
  72. {
  73. Object[] obj = new Object[] { "", "" };
  74. DataRow dr = dt.NewRow();
  75. dr.ItemArray = obj;
  76. dt.Rows.InsertAt(dr, 0);
  77. }
  78. uce.DataSource = dt;
  79. uce.ValueMember = valueMember;
  80. SetComboItemHeight(uce);
  81. }
  82. }
  83. /// <summary>
  84. /// 设置GRID行的颜色
  85. /// </summary>
  86. /// <param name="ultragrid">要设置的GRID</param>
  87. public static void SetGridRowColor(UltraGrid ultragrid)
  88. {
  89. if (ultragrid == null)
  90. return;
  91. foreach (UltraGridRow row in ultragrid.Rows)
  92. {
  93. if (row.Cells["VALIDFLAG"].Value.ToString().ToUpper() != "TRUE" && row.Cells["VALIDFLAG"].Value.ToString() != "有效")
  94. {
  95. row.Appearance.ForeColor = Color.Red;
  96. }
  97. else
  98. {
  99. row.Appearance.ForeColor = Color.Black;
  100. }
  101. if (row.HasChild() && row.ChildBands[0].Rows.Count > 0) //主从表--子表
  102. {
  103. foreach (UltraGridRow crow in row.ChildBands[0].Rows)
  104. {
  105. if (crow.Cells["VALIDFLAG"].Value.ToString().ToUpper() != "TRUE" && crow.Cells["VALIDFLAG"].Value.ToString() != "有效")
  106. {
  107. crow.Appearance.ForeColor = Color.Red;
  108. }
  109. else
  110. {
  111. crow.Appearance.ForeColor = Color.Black;
  112. }
  113. }
  114. }
  115. }
  116. }
  117. /// <summary>
  118. ///设置GRID不可编辑 不带选择框
  119. /// </summary>
  120. /// <param name="ug">GRID</param>
  121. public static void SetGridActivateOnly(UltraGrid ug)
  122. {
  123. if (ug == null)
  124. return;
  125. foreach (UltraGridRow row in ug.Rows)
  126. {
  127. for (int i = 0; i < row.Cells.Count; i++)
  128. row.Cells[i].Activation = Activation.ActivateOnly;
  129. }
  130. }
  131. /// <summary>
  132. /// 设置UltraComboEditor中的中文和非中文统一高度。
  133. /// </summary>
  134. /// <param name="cmb"></param>
  135. public static void SetComboItemHeight(UltraComboEditor cmb)
  136. {
  137. foreach (ValueListItem item in cmb.Items)
  138. {
  139. if (Regex.IsMatch(item.DisplayText, @"[\u4e00-\u9fa5]+"))
  140. {
  141. item.Appearance.FontData.SizeInPoints = 9.0F;
  142. }
  143. else
  144. {
  145. item.Appearance.FontData.SizeInPoints = 10.5F;
  146. }
  147. }
  148. }
  149. /// <summary>
  150. /// 用于基础信息表的操作后的定位
  151. /// </summary>
  152. /// <param name="ultragrid">要定位的GRID</param>
  153. /// <param name="colName">要定位的列名</param>
  154. /// <param name="keyword">关键字</param>
  155. public static void Postioning(UltraGrid ultragrid, string colName, string keyword)
  156. {
  157. foreach (UltraGridRow row in ultragrid.Rows)
  158. {
  159. if (row.Cells[colName].Value.ToString() == keyword)
  160. {
  161. row.Activate();
  162. break;
  163. }
  164. }
  165. }
  166. /// <summary>
  167. /// GRID列自适应 除了备注列
  168. /// </summary>
  169. /// <param name="ug">GRID</param>
  170. public static void SetColAutoSizeExceptMemo(UltraGrid ug)
  171. {
  172. UltraGridColumn[] col = new UltraGridColumn[] { ug.DisplayLayout.Bands[0].Columns["MEMO"] };
  173. GridHelper.RefreshAndAutoSizeExceptColumns(ug, col);
  174. }
  175. /// <summary>
  176. /// 过滤主表中没有但从表有的记录
  177. /// </summary>
  178. /// <param name="dt1">主表</param>
  179. /// <param name="dt2">从表</param>
  180. /// <param name="condition">关联主键</param>
  181. public static DataTable FilterTable(DataTable dt1, DataTable dt2, string condition)
  182. {
  183. if (dt1 != null && dt1.Rows.Count > 0 && dt2 != null)
  184. {
  185. for (int i = 0; i < dt2.Rows.Count; i++)
  186. {
  187. string conditionValue = dt2.Rows[i][condition].ToString();
  188. DataRow[] dr = dt1.Select(condition + "='" + conditionValue + "'");
  189. if (dr == null || dr.Length == 0)
  190. dt2.Rows[i].Delete();
  191. }
  192. dt2.AcceptChanges();
  193. return dt2;
  194. }
  195. return null;
  196. }
  197. /// <summary>
  198. /// 将下拉框绑定到GRID列
  199. /// </summary>
  200. /// <param name="uce">下拉框(已经初始化完成)</param>
  201. /// <param name="ColumnName">列名</param>
  202. /// <param name="con">空间集合(每次只需填入this.Controls)</param>
  203. /// <param name="ug">GRID</param>
  204. /// <param name="i">GRID的第几层结构</param>
  205. public static void BindColumn(UltraComboEditor uce, string ColumnName, System.Windows.Forms.Control.ControlCollection con, UltraGrid ug, int i)
  206. {
  207. con.Add(uce);
  208. uce.Visible = false;
  209. ug.DisplayLayout.Bands[i].Columns[ColumnName].EditorComponent = uce;
  210. ug.DisplayLayout.Bands[i].Columns[ColumnName].Style = Infragistics.Win.UltraWinGrid.ColumnStyle.DropDown;
  211. SetComboItemHeight(uce);
  212. }
  213. /// <summary>
  214. /// 绑定生成一个有树结构的下拉菜单
  215. /// </summary>
  216. /// <param name= "dtNodeSets "> 菜单记录数据所在的表 </param>
  217. /// <param name= "strParentColumn "> 表中用于标记父记录的字段 </param>
  218. /// <param name= "strRootValue "> 第一层记录的父记录值(通常设计为0或者-1或者Null)用来表示没有父记录 </param>
  219. /// <param name= "strIndexColumn "> 索引字段,也就是放在DropDownList的Value里面的字段 </param>
  220. /// <param name= "strTextColumn "> 显示文本字段,也就是放在DropDownList的Text里面的字段 </param>
  221. /// <param name= "drpBind "> 需要绑定的DropDownList </param>
  222. /// <param name= "i "> 用来控制缩入量的值,请输入-1 </param>
  223. public static void MakeTree(DataTable dtNodeSets, string strParentColumn, string strRootValue, string strIndexColumn, string strTextColumn, TreeNodeCollection drpBind, int i)
  224. {
  225. //每向下一层,多一个缩入单位
  226. i++;
  227. DataView dvNodeSets = new DataView(dtNodeSets);
  228. string filter = string.IsNullOrEmpty(strRootValue) ? strParentColumn + " is null" : string.Format(strParentColumn + "='{0}'", strRootValue);
  229. dvNodeSets.RowFilter = filter;
  230. string strPading = " "; //缩入字符
  231. //通过i来控制缩入字符的长度,我这里设定的是一个全角的空格
  232. for (int j = 0; j < i; j++)
  233. strPading += " ";//如果要增加缩入的长度,改成两个全角的空格就可以了
  234. foreach (DataRowView drv in dvNodeSets)
  235. {
  236. TreeNode tnNode = new TreeNode();
  237. tnNode = new TreeNode();//建立一个新节点(学名叫:一个实例)
  238. tnNode.Tag = drv[strIndexColumn].ToString();//节点的Value值,一般为数据库的id值
  239. tnNode.Text = drv[strTextColumn].ToString();//节点的Text,节点的文本显示
  240. if (!string.IsNullOrEmpty(drv["PID"].ToString()))
  241. {
  242. tnNode.ToolTipText = drv["PID"].ToString();
  243. }
  244. else
  245. {
  246. tnNode.ImageIndex = 0;
  247. }
  248. //ListItem li = new ListItem(strPading + "|- " + drv[strTextColumn].ToString(), drv[strIndexColumn].ToString());
  249. drpBind.Add(tnNode);
  250. MakeTree(dtNodeSets, strParentColumn, drv[strIndexColumn].ToString(), strIndexColumn, strTextColumn, tnNode.Nodes, i);
  251. }
  252. //递归结束,要回到上一层,所以缩入量减少一个单位
  253. i--;
  254. }
  255. /// <summary>
  256. /// 时间计算返回月
  257. /// </summary>
  258. /// <param name="startTime">开始时间</param>
  259. /// <param name="endTime">结束时间</param>
  260. /// <returns></returns>
  261. public static int getMonth(DateTime startTime, DateTime endTime)
  262. {
  263. int year1 = startTime.Year;
  264. int year2 = endTime.Year;
  265. int month1 = startTime.Month;
  266. int month2 = endTime.Month;
  267. int months = 12 * (year2 - year1) + (month2 - month1);
  268. return months;
  269. }
  270. }
  271. }