BaseUtil.cs 32 KB

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