BaseUtil.cs 34 KB

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