BaseUtil.cs 30 KB

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