BaseUtil.cs 30 KB

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