BaseUtil.cs 29 KB

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