BaseUtil.cs 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.Data;
  5. using System.Text;
  6. using System.Windows.Forms;
  7. using UAS_LabelMachine.CustomControl;
  8. using UAS_LabelMachine.CustomControl.GroupBoxWithBorder;
  9. using static System.Windows.Forms.Control;
  10. using System.Text.RegularExpressions;
  11. using Seagull.BarTender.Print;
  12. using Microsoft.CSharp;
  13. using System.CodeDom.Compiler;
  14. using System.Reflection;
  15. using UAS_LabelMachine.Entity;
  16. namespace UAS_LabelMachine
  17. {
  18. class BaseUtil
  19. {
  20. static string SysDisc;
  21. public static string SysDisc1
  22. {
  23. get
  24. {
  25. return SysDisc = Environment.GetEnvironmentVariable("windir").Substring(0, 1);
  26. }
  27. set
  28. {
  29. SysDisc = value;
  30. }
  31. }
  32. static int serialnum = 0;
  33. static bool FirstCode = false;
  34. //生成唯一条码
  35. public static string BarcodeMethod1(string pd_id, string pr_id, string pib_barcode)
  36. {
  37. if (pib_barcode != "")
  38. {
  39. if (FirstCode)
  40. {
  41. serialnum = serialnum + 1;
  42. }
  43. //第一次的时候去获取数据库查询出来的值
  44. else
  45. {
  46. serialnum = int.Parse(pib_barcode.Substring(pib_barcode.Length - 4)) + 1;
  47. FirstCode = true;
  48. }
  49. }
  50. else
  51. {
  52. serialnum = serialnum + 1;
  53. }
  54. string serialcode = serialnum.ToString();
  55. for (int i = serialnum.ToString().Length; i < 4; i++)
  56. {
  57. serialcode = "0" + serialcode;
  58. }
  59. return pd_id + "-" + pr_id + "-" + serialcode;
  60. }
  61. /// <summary>
  62. /// 筛选DataTable
  63. /// </summary>
  64. /// <param name="dt"></param>
  65. /// <param name="condition"></param>
  66. /// <returns></returns>
  67. public static DataTable filterDataTable(DataTable dt, String condition)
  68. {
  69. if (dt == null)
  70. return new DataTable();
  71. //获取筛选条件中的列名,值
  72. DataRow[] dataRows = dt.Select(condition);
  73. DataTable ndt = dt.Clone();
  74. for (int i = 0; i < dataRows.Length; i++)
  75. {
  76. ndt.Rows.Add(dataRows[i].ItemArray);
  77. }
  78. return ndt;
  79. }
  80. public static string DToAny(double DB, int Type)
  81. {
  82. string H = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
  83. long D;
  84. double B;
  85. string tempD = "", tempB = "";
  86. D = (long)DB;
  87. B = DB - D;
  88. if (D == 0)
  89. {
  90. tempD = "0";
  91. }
  92. while (D != 0)
  93. {
  94. tempD = H[(((int)D % Type))] + tempD;
  95. D = D / Type;
  96. }
  97. for (int i = 0; i < 7; i++)
  98. {
  99. if (B == 0)
  100. {
  101. break;
  102. }
  103. tempB += H[((int)(B * Type))];
  104. B = B * Type - (int)(B * Type);
  105. }
  106. if (tempB == "")
  107. {
  108. return tempD;
  109. }
  110. else
  111. {
  112. return tempD + "." + tempB;
  113. }
  114. }
  115. public static DataTable GetExportDataTable(DataGridView dgv)
  116. {
  117. DataTable dt = ((DataTable)dgv.DataSource).Copy();
  118. for (int i = 0; i < dt.Columns.Count; i++)
  119. {
  120. for (int j = 0; j < dgv.Columns.Count; j++)
  121. {
  122. if (dt.Columns[i].ColumnName.ToLower() == dgv.Columns[j].DataPropertyName.ToLower())
  123. {
  124. dt.Columns[i].ColumnName = dgv.Columns[j].HeaderText;
  125. }
  126. }
  127. }
  128. return dt;
  129. }
  130. /// <summary>
  131. /// 通过DataTable的ColumnName和Caption来拼接一条语句
  132. /// </summary>
  133. /// <param name=""></param>
  134. /// <returns></returns>
  135. public static string GetGridViewSelectContent(DataGridView d)
  136. {
  137. StringBuilder selectConetnt = new StringBuilder();
  138. DataTable dt = (DataTable)d.DataSource;
  139. if (dt == null)
  140. {
  141. foreach (DataGridViewColumn dc in d.Columns)
  142. {
  143. if (dc.DataPropertyName != "" && dc.DataPropertyName != null)
  144. {
  145. selectConetnt.Append(dc.DataPropertyName + " as " + dc.DataPropertyName + ",");
  146. }
  147. }
  148. }
  149. else
  150. {
  151. foreach (DataColumn dc in dt.Columns)
  152. {
  153. selectConetnt.Append(dc.Caption + " as " + dc.ColumnName + ",");
  154. }
  155. }
  156. return selectConetnt.Remove(selectConetnt.Length - 1, 1).ToString();
  157. }
  158. /// <summary>
  159. /// 通过字段和其展示的中文值获取查询的内容
  160. /// </summary>
  161. /// <param name="field"></param>
  162. /// <param name="cnfield"></param>
  163. /// <returns></returns>
  164. public static string GetSelectContentByStringArray(string[] field, string[] cnfield)
  165. {
  166. StringBuilder sb = new StringBuilder();
  167. for (int i = 0; i < field.Length; i++)
  168. {
  169. sb.Append(field[i] + " as " + cnfield[i] + ",");
  170. }
  171. //去掉多余的逗号
  172. sb.Remove(sb.Length - 1, 1);
  173. return sb.ToString();
  174. }
  175. /// <summary>
  176. /// 传入控件的集合和DataTable,通过判断控件的名称和数据源的列的描述来匹配,支持单层的GroupBox和Panel
  177. /// </summary>
  178. /// <param name="collection"></param>
  179. /// <param name="dt"></param>
  180. public static void SetFormValue(ControlCollection collection, DataTable dt)
  181. {
  182. //DataTable存在数据才进行赋值操作
  183. if (dt.Rows.Count > 0)
  184. {
  185. for (int i = 0; i < collection.Count; i++)
  186. {
  187. string controlName = collection[i].Name;
  188. //默认给TextBox和Label赋值
  189. if (collection[i] is TextBox || collection[i] is Label || collection[i] is SearchTextBox || collection[i] is CheckBox)
  190. {
  191. for (int j = 0; j < dt.Columns.Count; j++)
  192. {
  193. if (controlName.ToUpper() == dt.Columns[j].Caption.ToUpper())
  194. {
  195. //字段含有Status内容的才进行转换
  196. if (controlName.ToUpper().Contains("STATUS"))
  197. {
  198. //对审批状态进行判断
  199. switch (dt.Rows[0][j].ToString().ToUpper())
  200. {
  201. case "ENTERING":
  202. collection[i].Text = "在录入";
  203. break;
  204. case "UNAPPROVED":
  205. collection[i].Text = "未批准";
  206. break;
  207. case "COMMITED":
  208. collection[i].Text = "已提交";
  209. break;
  210. case "APPROVE":
  211. collection[i].Text = "已批准";
  212. break;
  213. case "AUDITED":
  214. collection[i].Text = "已审核";
  215. break;
  216. case "STARTED":
  217. collection[i].Text = "已下发";
  218. break;
  219. case "UNCHECK":
  220. collection[i].Text = "待检验";
  221. break;
  222. case "CHECKING":
  223. collection[i].Text = "检验中";
  224. break;
  225. default:
  226. collection[i].Text = dt.Rows[0][j].ToString();
  227. break;
  228. }
  229. }
  230. else
  231. {
  232. if (collection[i] is CheckBox)
  233. {
  234. (collection[i] as CheckBox).Checked = dt.Rows[0][j].ToString() == "-1" ? true : false;
  235. }
  236. else
  237. {
  238. collection[i].Text = dt.Rows[0][j].ToString();
  239. }
  240. }
  241. }
  242. }
  243. }
  244. //对封装在GroupBox的和Panel的控件进行批量赋值
  245. if (collection[i] is GroupBox || collection[i] is Panel || collection[i] is GroupBoxWithBorder)
  246. {
  247. for (int j = 0; j < collection[i].Controls.Count; j++)
  248. {
  249. controlName = collection[i].Controls[j].Name;
  250. if (collection[i].Controls[j] is TextBox || collection[i].Controls[j] is Label || collection[i].Controls[j] is SearchTextBox)
  251. {
  252. for (int k = 0; k < dt.Columns.Count; k++)
  253. {
  254. if (controlName.ToUpper() == dt.Columns[k].Caption.ToUpper())
  255. {
  256. collection[i].Controls[j].Text = dt.Rows[0][k].ToString();
  257. }
  258. }
  259. }
  260. }
  261. }
  262. }
  263. }
  264. //如果没有记录的话,将dt中含有的列的对应值设置为空
  265. else
  266. {
  267. for (int i = 0; i < collection.Count; i++)
  268. {
  269. if (collection[i] is TextBox || collection[i] is Label || collection[i] is SearchTextBox)
  270. {
  271. for (int j = 0; j < dt.Columns.Count; j++)
  272. {
  273. if (collection[i].Name.ToUpper() == dt.Columns[j].Caption.ToUpper())
  274. {
  275. collection[i].Text = "";
  276. }
  277. }
  278. }
  279. }
  280. }
  281. }
  282. /// <summary>
  283. /// 获取标签的路径
  284. /// </summary>
  285. /// <param name="URL"></param>
  286. /// <param name="LabelName"></param>
  287. /// <returns></returns>
  288. public static string GetLabelUrl(string URL, string LabelName, DateTime time)
  289. {
  290. //如果是传入的数据是从FTP取的文件
  291. if (URL.Contains("ftp:"))
  292. {
  293. ftpOperater ftp = new ftpOperater(SystemInf.FTPModel);
  294. return ftp.Download(LabelName, time);
  295. }
  296. else
  297. {
  298. return URL;
  299. }
  300. }
  301. /// <summary>
  302. /// 从DGV获取指定的列的数据形式是数组的形式
  303. /// </summary>
  304. public static ArrayList[] GetColumnDataFromDGV(DataGridView dgv, string[] ColumnName)
  305. {
  306. ArrayList[] array = new ArrayList[ColumnName.Length];
  307. //实例化和查询参数个数一样的ArrayList
  308. for (int i = 0; i < ColumnName.Length; i++)
  309. {
  310. array[i] = new ArrayList();
  311. }
  312. DataTable dt = (DataTable)dgv.DataSource;
  313. //如果第一列是否选框的话
  314. if (dgv.Columns[0] is DataGridViewCheckBoxColumn)
  315. {
  316. for (int i = 0; i < dt.Rows.Count; i++)
  317. {
  318. if (dgv.Rows[i].Cells[0].FormattedValue.ToString() == "True")
  319. {
  320. for (int j = 0; j < ColumnName.Length; j++)
  321. {
  322. array[j].Add(dt.Rows[i][ColumnName[j]]);
  323. }
  324. }
  325. }
  326. }
  327. //否则直接获取全部的数据
  328. else
  329. {
  330. for (int i = 0; i < dgv.RowCount; i++)
  331. {
  332. for (int j = 0; j < ColumnName.Length; j++)
  333. {
  334. array[j].Add(dt.Rows[i][ColumnName[j]]);
  335. }
  336. }
  337. }
  338. return array;
  339. }
  340. /// <summary>
  341. /// 通过DataGridView和需要隐藏的字段的数组来对字段进行隐藏
  342. /// </summary>
  343. /// <param name="dgv"></param>
  344. /// <param name="field"></param>
  345. public static void HideField(DataGridView dgv, string[] field)
  346. {
  347. DataTable dt = (DataTable)dgv.DataSource;
  348. foreach (DataColumn dc in dt.Columns)
  349. {
  350. foreach (string s in field)
  351. {
  352. if (dc.Caption == s)
  353. {
  354. dgv.Columns[dc.ColumnName].Visible = false;
  355. }
  356. }
  357. }
  358. }
  359. /// <summary>
  360. /// 通过查询的内容获取到字段的描述
  361. /// </summary>
  362. /// <param name="field"></param>
  363. /// <returns></returns>
  364. public static string[] GetCaptionFromField(string field)
  365. {
  366. string[] caption = field.Split(',');
  367. for (int i = 0; i < caption.Length; i++)
  368. {
  369. caption[i] = caption[i].Substring(0, caption[i].LastIndexOf("as")).Trim();
  370. }
  371. return caption;
  372. }
  373. /// <summary>
  374. /// 通过查询的语句获取查询的字段
  375. /// </summary>
  376. /// <param name="field"></param>
  377. /// <returns></returns>
  378. public static string[] GetField(string field)
  379. {
  380. string[] fields = field.Split(',');
  381. for (int i = 0; i < fields.Length; i++)
  382. {
  383. fields[i] = fields[i].Substring(fields[i].LastIndexOf("as") + 2, fields[i].Length - fields[i].LastIndexOf("as") - 2).Trim();
  384. }
  385. return fields;
  386. }
  387. /// <summary>
  388. /// 将DataTable转换为List的键值对
  389. /// </summary>
  390. /// <param name="dt"></param>
  391. /// <returns></returns>
  392. public static List<Dictionary<string, string>> DataTableToListDictionary(DataTable dt)
  393. {
  394. List<Dictionary<string, string>> list = new List<Dictionary<string, string>>();
  395. foreach (DataRow dr in dt.Rows)
  396. {
  397. Dictionary<string, string> dictionary = new Dictionary<string, string>();
  398. foreach (DataColumn dc in dt.Columns)
  399. {
  400. dictionary.Add(dc.Caption, dr[dt.Columns.IndexOf(dc)].ToString());
  401. }
  402. list.Add(dictionary);
  403. }
  404. return list;
  405. }
  406. /// <summary>
  407. /// 通过描述取DataTable的列名,主要用于从配置中取数据
  408. /// </summary>
  409. /// <param name="dt"></param>
  410. /// <param name="caption"></param>
  411. /// <returns></returns>
  412. public static string GetColumnNameByCaption(DataTable dt, string caption)
  413. {
  414. foreach (DataColumn dc in dt.Columns)
  415. {
  416. if (dc.Caption.ToLower() == caption)
  417. {
  418. return dc.ColumnName;
  419. }
  420. }
  421. return null;
  422. }
  423. //用于封装异常,也可以用于错误的提示
  424. public static void ShowError(string errorMessage)
  425. {
  426. throw new Exception(errorMessage);
  427. }
  428. /// <summary>
  429. /// 清除DataTable的结构和数据,清除列结构时需要从最后的一列开始删
  430. /// </summary>
  431. /// <param name="dt"></param>
  432. public static void CleanDataTable(DataTable dt)
  433. {
  434. for (int i = dt.Columns.Count - 1; i >= 0; i--)
  435. {
  436. dt.Columns.Remove(dt.Columns[i]);
  437. }
  438. }
  439. /// <summary>
  440. /// 不清楚表结构,只清楚数据
  441. /// </summary>
  442. /// <param name="dt"></param>
  443. public static void CleanDataTableData(DataTable dt)
  444. {
  445. for (int i = dt.Rows.Count - 1; i >= 0; i--)
  446. {
  447. dt.Rows.Remove(dt.Rows[i]);
  448. }
  449. }
  450. /// <summary>
  451. /// 获取拼接的字段
  452. /// </summary>
  453. /// <param name="dt"></param>
  454. /// <returns></returns>
  455. public static string GetFieldFromDataTable(DataTable dt)
  456. {
  457. StringBuilder sb = new StringBuilder();
  458. foreach (DataColumn dc in dt.Columns)
  459. {
  460. sb.Append(dc.Caption + ",");
  461. }
  462. return sb.ToString().Substring(sb.ToString().Length - 1, 1);
  463. }
  464. /// <summary>
  465. /// 已经定义好的DataGridView绑定数据,operate是用来添加操作列的
  466. /// </summary>
  467. /// <param name="dgv"></param>
  468. /// <param name="dt"></param>
  469. /// <param name="AddOpetateColumn"></param>
  470. /// <param name="operate"></param>
  471. public static void FillDgvWithDataTable(DataGridView dgv, DataTable dt, params DataGridViewImageColumn[] operate)
  472. {
  473. dgv.AutoGenerateColumns = false;
  474. dgv.DataSource = dt;
  475. if (operate.Length > 0)
  476. {
  477. if (dgv.Columns[operate[0].Name] != null)
  478. {
  479. dgv.Columns.Remove(dgv.Columns[operate[0].Name]);
  480. }
  481. dgv.Columns.Add(operate[0]);
  482. }
  483. //纯英文的列不予展示
  484. //Regex regEnglish = new Regex("^[_][a-z]$");
  485. //foreach (DataGridViewColumn dgvc in dgv.Columns)
  486. //{
  487. // if (dgvc.HeaderText.IndexOf("_id") > 0)
  488. // {
  489. // dgvc.Visible = false;
  490. // }
  491. //}
  492. }
  493. /// <summary>
  494. /// 清除DataGridView的数据
  495. /// </summary>
  496. /// <param name="dgv"></param>
  497. public static void CleanDGVData(DataGridView dgv)
  498. {
  499. for (int i = dgv.Rows.Count - 1; i >= 0; i--)
  500. {
  501. dgv.Rows.RemoveAt(i);
  502. }
  503. }
  504. /// <summary>
  505. /// 清除Form的指定类型的数据
  506. /// </summary>
  507. /// <param name="Form"></param>
  508. public static void CleanForm(Form Form)
  509. {
  510. for (int i = 0; i < Form.Controls.Count; i++)
  511. {
  512. if (Form.Controls[i] is EnterTextBox || Form.Controls[i] is TextBox || Form.Controls[i] is RichTextBox || Form.Controls[i] is SearchTextBox)
  513. Form.Controls[i].Text = "";
  514. if (Form.Controls[i] is DataGridView)
  515. CleanDGVData((DataGridView)Form.Controls[i]);
  516. }
  517. }
  518. /// <summary>
  519. /// 用于给DGV中的Combox列赋静态值
  520. /// </summary>
  521. /// <param name="dgvc"></param>
  522. /// <param name="displayField"></param>
  523. /// <param name="valueField"></param>
  524. /// <param name="Value"></param>
  525. /// <returns></returns>
  526. public static void SetDgvColumnComboxData(DataGridViewComboBoxColumn dgvc, string DataPropertyName, string displayField, string valueField, string[] Value)
  527. {
  528. DataTable dt = new DataTable();
  529. dt.Columns.Add(displayField);
  530. dt.Columns.Add(valueField);
  531. for (int i = 0; i < Value.Length; i++)
  532. {
  533. DataGridViewRow row = new DataGridViewRow();
  534. dt.Rows.Add(row);
  535. dt.Rows[i][displayField] = Value[i].Split('#')[0];
  536. dt.Rows[i][valueField] = Value[i].Split('#')[1];
  537. }
  538. dgvc.DataPropertyName = DataPropertyName;
  539. dgvc.DataSource = dt;
  540. dgvc.DisplayMember = displayField;
  541. dgvc.ValueMember = valueField;
  542. }
  543. /// <summary>
  544. /// 用于给DGV中的ComboxCell赋静态值
  545. /// </summary>
  546. /// <param name="dgvcc"></param>
  547. /// <param name="displayField"></param>
  548. /// <param name="valueField"></param>
  549. /// <param name="Value"></param>
  550. public static void SetDGVCellComboxData(DataGridViewComboBoxCell dgvcc, string displayField, string valueField, string[] Value)
  551. {
  552. DataTable dt = new DataTable();
  553. dt.Columns.Add(displayField);
  554. dt.Columns.Add(valueField);
  555. for (int i = 0; i < Value.Length; i++)
  556. {
  557. DataRow dr = dt.NewRow();
  558. dr[displayField] = Value[i].Split('#')[0];
  559. dr[valueField] = Value[i].Split('#')[1];
  560. dt.Rows.Add(dr);
  561. }
  562. dgvcc.DisplayMember = displayField;
  563. dgvcc.ValueMember = valueField;
  564. dgvcc.DataSource = dt;
  565. }
  566. /// <summary>
  567. /// 用于给DGV中的ComboxCell赋静态值
  568. /// </summary>
  569. /// <param name="dgvcc"></param>
  570. /// <param name="displayField"></param>
  571. /// <param name="valueField"></param>
  572. /// <param name="Value"></param>
  573. public static void SetComboxData(ComboBox dgvcc, string displayField, string valueField, string[] Value)
  574. {
  575. DataTable dt = new DataTable();
  576. dt.Columns.Add(displayField);
  577. dt.Columns.Add(valueField);
  578. for (int i = 0; i < Value.Length; i++)
  579. {
  580. DataRow dr = dt.NewRow();
  581. dr[displayField] = Value[i].Split('#')[0];
  582. dr[valueField] = Value[i].Split('#')[1];
  583. dt.Rows.Add(dr);
  584. }
  585. dgvcc.DisplayMember = displayField;
  586. dgvcc.ValueMember = valueField;
  587. dgvcc.DataSource = dt;
  588. }
  589. /// <summary>
  590. /// 获取刷选的SQL语句,传入的是TextBox的Control,传入的SQL不带Where条件
  591. /// </summary>
  592. /// <param name="SQL"></param>
  593. /// <param name="Condition"></param>
  594. /// <returns></returns>
  595. public static string GetScreenSqlCondition(params Control[] Condition)
  596. {
  597. string condition = " where ";
  598. for (int i = 0; i < Condition.Length; i++)
  599. {
  600. if (i != Condition.Length - 1)
  601. {
  602. if (Condition[i] is ComboBox)
  603. {
  604. condition += "(" + Condition[i].Tag + " like " + "'%" + (Condition[i] as ComboBox).SelectedValue + "%' or " + Condition[i].Tag + " is null) and ";
  605. }
  606. else
  607. {
  608. condition += "(" + Condition[i].Tag + " like " + "'%" + Condition[i].Text + "%' or " + Condition[i].Tag + " is null) and ";
  609. }
  610. }
  611. else
  612. {
  613. if (Condition[i] is ComboBox)
  614. {
  615. condition += "(" + Condition[i].Tag + " like " + "'%" + (Condition[i] as ComboBox).SelectedValue + "%' or " + Condition[i].Tag + " is null)";
  616. }
  617. else
  618. {
  619. condition += "(" + Condition[i].Tag + " like " + "'%" + Condition[i].Text + "%' or " + Condition[i].Tag + " is null)";
  620. }
  621. }
  622. }
  623. return condition;
  624. }
  625. public static void CleanDataGridView(DataGridView dgv)
  626. {
  627. for (int i = dgv.Columns.Count - 1; i >= 0; i--)
  628. {
  629. dgv.Columns.RemoveAt(i);
  630. }
  631. }
  632. /// <summary>
  633. /// 取出SQL中的参数占位符
  634. /// </summary>
  635. /// <param name="SQL"></param>
  636. /// <returns></returns>
  637. public static string[] GetParamFromSQL(string SQL)
  638. {
  639. string[] par = SQL.Split(':');
  640. //用来存参数的数组
  641. StringBuilder[] addpar = new StringBuilder[par.Length - 1];
  642. string[] param = new string[par.Length - 1];
  643. for (int i = 0; i < par.Length - 1; i++)
  644. {
  645. //新建一个char类型的数组用来存储每个字节的变量
  646. char[] c = par[i + 1].ToCharArray();
  647. addpar[i] = new StringBuilder();
  648. for (int j = 0; j < c.Length; j++)
  649. {
  650. if (c[j] != ' ' && c[j] != ',' && c[j] != ')')
  651. {
  652. addpar[i].Append(c[j]);
  653. }
  654. else
  655. {
  656. break;
  657. }
  658. }
  659. }
  660. for (int i = 0; i < par.Length - 1; i++)
  661. {
  662. param[i] = addpar[i].ToString();
  663. }
  664. return param;
  665. }
  666. public static void SetFormCenter(Form form)
  667. {
  668. form.StartPosition = FormStartPosition.CenterParent;
  669. }
  670. /// <summary>
  671. /// 设置DataGridView的指定列可编辑
  672. /// </summary>
  673. /// <param name="DGV"></param>
  674. /// <param name="EditAbleField"></param>
  675. public static void SetDataGridViewReadOnly(DataGridView DGV, string[] EditAbleField)
  676. {
  677. foreach (DataGridViewColumn dc in DGV.Columns)
  678. {
  679. dc.ReadOnly = true;
  680. foreach (string s in EditAbleField)
  681. {
  682. if (dc.Name.ToLower() == s.ToLower())
  683. {
  684. DGV.Columns[dc.Name].ReadOnly = false;
  685. }
  686. }
  687. }
  688. }
  689. //判断带有CheckBox的DGV是否有项目勾选了
  690. public static DataTable DGVIfChecked(DataGridView dgv)
  691. {
  692. int CheckCount = 0;
  693. DataTable dt = new DataTable();
  694. //第一列是勾选框,排除在循环之外
  695. for (int i = 1; i < dgv.Columns.Count; i++)
  696. {
  697. dt.Columns.Add(dgv.Columns[i].Name);
  698. }
  699. for (int i = 0; i < dgv.RowCount; i++)
  700. {
  701. if (dgv.Rows[i].Cells[0].Value != null)
  702. {
  703. if (dgv.Rows[i].Cells[0].Value.ToString() == "True")
  704. {
  705. if (dgv.Rows[i].Tag.ToString() == "SonRow")
  706. {
  707. DataRow dr = dt.NewRow();
  708. for (int j = 1; j < dgv.ColumnCount; j++)
  709. {
  710. dr[dgv.Columns[j].Name] = dgv.Rows[i].Cells[j].FormattedValue;
  711. }
  712. dt.Rows.Add(dr);
  713. CheckCount++;
  714. }
  715. }
  716. }
  717. }
  718. //判断是否勾选了明细
  719. if (CheckCount == 0)
  720. {
  721. return null;
  722. }
  723. return dt;
  724. }
  725. /// <summary>
  726. /// 设置只允许输入数字
  727. /// </summary>
  728. /// <param name="sender"></param>
  729. /// <param name="e"></param>
  730. public static void NumOnly(object sender, KeyPressEventArgs e)
  731. {
  732. if (e.KeyChar != '\b')//这是允许输入退格键
  733. {
  734. if ((e.KeyChar < '0') || (e.KeyChar > '9'))//这是允许输入0-9数字
  735. {
  736. e.Handled = true;
  737. }
  738. }
  739. }
  740. public static Dictionary<string, object> ToDictionary(string JsonData)
  741. {
  742. object Data = null;
  743. Dictionary<string, object> Dic = new Dictionary<string, object>();
  744. if (JsonData.StartsWith("["))
  745. {
  746. //如果目标直接就为数组类型,则将会直接输出一个Key为List的List<Dictionary<string, object>>集合
  747. //使用示例List<Dictionary<string, object>> ListDic = (List<Dictionary<string, object>>)Dic["List"];
  748. List<Dictionary<string, object>> List = new List<Dictionary<string, object>>();
  749. MatchCollection ListMatch = Regex.Matches(JsonData, @"{[\s\S]+?}");//使用正则表达式匹配出JSON数组
  750. foreach (Match ListItem in ListMatch)
  751. {
  752. List.Add(ToDictionary(ListItem.ToString()));//递归调用
  753. }
  754. Data = List;
  755. Dic.Add("List", Data);
  756. }
  757. else
  758. {
  759. MatchCollection Match = Regex.Matches(JsonData, @"""(.+?)"": {0,1}(\[[\s\S]+?\]|null|"".+?""|-{0,1}\d*)");//使用正则表达式匹配出JSON数据中的键与值
  760. foreach (Match item in Match)
  761. {
  762. try
  763. {
  764. if (item.Groups[2].ToString().StartsWith("["))
  765. {
  766. //如果目标是数组,将会输出一个Key为当前Json的List<Dictionary<string, object>>集合
  767. //使用示例List<Dictionary<string, object>> ListDic = (List<Dictionary<string, object>>)Dic["Json中的Key"];
  768. List<Dictionary<string, object>> List = new List<Dictionary<string, object>>();
  769. MatchCollection ListMatch = Regex.Matches(item.Groups[2].ToString(), @"{[\s\S]+?}");//使用正则表达式匹配出JSON数组
  770. foreach (Match ListItem in ListMatch)
  771. {
  772. List.Add(ToDictionary(ListItem.ToString()));//递归调用
  773. }
  774. Data = List;
  775. }
  776. else if (item.Groups[2].ToString().ToLower() == "null") Data = null;//如果数据为null(字符串类型),直接转换成null
  777. else Data = item.Groups[2].ToString(); //数据为数字、字符串中的一类,直接写入
  778. Dic.Add(item.Groups[1].ToString(), Data);
  779. }
  780. catch { }
  781. }
  782. }
  783. return Dic;
  784. }
  785. public static string AddField(string[] Fields)
  786. {
  787. string sql = " ";
  788. foreach (string field in Fields)
  789. {
  790. sql += field + ",";
  791. }
  792. return sql.Substring(0, sql.Length - 1);
  793. }
  794. public static string GetArrStr(ArrayList<string> arr, string Split)
  795. {
  796. string Str = "";
  797. for (int i = 0; i < arr.ToArray().Length; i++)
  798. {
  799. Str += arr.ToArray()[i] + Split;
  800. }
  801. return Str;
  802. }
  803. //设置控件不可见
  804. public static void SetControlsVisible(params object[] sender)
  805. {
  806. foreach (object item in sender)
  807. {
  808. (item as DataGridViewColumn).Visible = false;
  809. }
  810. }
  811. public static string GetOutBoxSQL(LabelFormatDocument OutFormat, DataTable OutLabelParam, string pi_inoutno, string pib_id, string pib_outboxcode2, bool iCustProdCode, bool iCustPo, bool iDC, bool iLotNo, bool iOrderCode)
  812. {
  813. StringBuilder sql = new StringBuilder();
  814. sql.Clear();
  815. for (int j = 0; j < OutFormat.SubStrings.Count; j++)
  816. {
  817. DataRow[] dr1 = OutLabelParam.Select("lp_name='" + OutFormat.SubStrings[j].Name + "'");
  818. if (dr1.Length > 0 && (dr1[0]["lp_valuetype"].ToString() == "字符串" || dr1[0]["lp_valuetype"].ToString() == "text") && dr1[0]["lp_sql"].ToString() != "")
  819. {
  820. sql.Append(dr1[0]["lp_sql"].ToString() + ",");
  821. }
  822. }
  823. //界面设定的分组条件
  824. string GroupByCondition = "";
  825. if (iCustProdCode)
  826. {
  827. GroupByCondition += "pd_custprodcode,";
  828. }
  829. if (iCustPo)
  830. {
  831. GroupByCondition += "pd_pocode,";
  832. }
  833. if (iDC)
  834. {
  835. GroupByCondition += "pib_datecode,";
  836. }
  837. if (iLotNo)
  838. {
  839. GroupByCondition += "pib_lotno,";
  840. }
  841. if (iOrderCode)
  842. {
  843. GroupByCondition += "pd_ordercode,";
  844. }
  845. if (iCustProdCode || iCustPo || iDC || iLotNo || iOrderCode)
  846. {
  847. GroupByCondition = " group by " + (GroupByCondition.Substring(0, GroupByCondition.Length - 1));
  848. }
  849. sql.Append("max(pib_id),");
  850. return "select " + sql.ToString().Substring(0, sql.Length - 1) + " from prodiobarcode where pib_inoutno = '" + pi_inoutno + "' and pib_outboxcode2 = " + (pib_outboxcode2 == "" ? "0" : pib_outboxcode2) + GroupByCondition;
  851. }
  852. public static void CustomerInit(string oContent)
  853. {
  854. if (oContent == ""|| oContent=="null"|| oContent==" ")
  855. return;
  856. //创建编译环境实例
  857. CSharpCodeProvider objCSharpCodePrivoder = new CSharpCodeProvider();
  858. //UAS_LabelMachine
  859. CompilerParameters objCompilerParameters = new CompilerParameters();
  860. objCompilerParameters.ReferencedAssemblies.Add("System.dll");
  861. objCompilerParameters.ReferencedAssemblies.Add("System.Windows.Forms.dll");
  862. objCompilerParameters.GenerateExecutable = false;
  863. objCompilerParameters.GenerateInMemory = true;
  864. // 4.CompilerResults
  865. CompilerResults cr = objCSharpCodePrivoder.CompileAssemblyFromSource(objCompilerParameters, oContent);
  866. if (cr.Errors.HasErrors)
  867. {
  868. foreach (CompilerError err in cr.Errors)
  869. {
  870. Console.WriteLine(err.ErrorText);
  871. }
  872. }
  873. else
  874. {
  875. // 通过反射,调用HelloWorld的实例
  876. Assembly objAssembly = cr.CompiledAssembly;
  877. object objHelloWorld = objAssembly.CreateInstance("CustInitSpace.CustInitClass", true);
  878. MethodInfo objMI = objHelloWorld.GetType().GetMethod("CustInit");
  879. try
  880. {
  881. Console.WriteLine(objMI.Invoke(objHelloWorld, null));
  882. }
  883. catch (Exception ex)
  884. {
  885. Console.WriteLine(ex.Message);
  886. }
  887. }
  888. }
  889. }
  890. namespace CustInitSpace
  891. {
  892. using System;
  893. using System.Windows.Forms;
  894. public class CustInitClass
  895. {
  896. public void CustInit()
  897. {
  898. Form f = Form.ActiveForm;
  899. (f.Controls["LabelInf"] as DataGridView).Columns["pib_year"].Visible = false;
  900. (f.Controls["LabelInf"] as DataGridView).Columns["pib_month"].Visible = false;
  901. (f.Controls["LabelInf"] as DataGridView).Columns["pib_day"].Visible = false;
  902. (f.Controls["LabelInf"] as DataGridView).Columns["pib_custoutboxcode"].HeaderText = "芯片号";
  903. }
  904. }
  905. }
  906. }