BaseUtil.cs 32 KB

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