BaseUtil.cs 30 KB

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