BaseUtil.cs 31 KB

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