BaseUtil.cs 33 KB

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