BaseUtil.cs 38 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988
  1. using LabelManager2;
  2. using System;
  3. using System.Collections;
  4. using System.Collections.Generic;
  5. using System.ComponentModel;
  6. using System.Data;
  7. using System.Drawing;
  8. using System.IO;
  9. using System.Net;
  10. using System.Net.Sockets;
  11. using System.Reflection;
  12. using System.Text;
  13. using System.Text.RegularExpressions;
  14. using System.Windows.Forms;
  15. using System.Windows.Forms.DataVisualization.Charting;
  16. using UAS_MES.CustomControl.DataGrid_View;
  17. using UAS_MES.CustomControl.GroupBoxWithBorder;
  18. using UAS_MES.CustomControl.TextBoxWithIcon;
  19. using UAS_MES.CustomControl.ValueLabel;
  20. using UAS_MES.Entity;
  21. using static System.Windows.Forms.Control;
  22. namespace UAS_MES.PublicMethod
  23. {
  24. class BaseUtil
  25. {
  26. /// <summary>
  27. /// 通过DataTable的ColumnName和Caption来拼接一条语句
  28. /// </summary>
  29. /// <param name=""></param>
  30. /// <returns></returns>
  31. public static string GetGridViewSelectContent(DataGridView d)
  32. {
  33. StringBuilder selectConetnt = new StringBuilder();
  34. DataTable dt = (DataTable)d.DataSource;
  35. if (dt == null)
  36. {
  37. foreach (DataGridViewColumn dc in d.Columns)
  38. {
  39. if (dc.DataPropertyName != "" && dc.DataPropertyName != null)
  40. {
  41. selectConetnt.Append(dc.Name + " as " + dc.Name + ",");
  42. }
  43. }
  44. }
  45. else
  46. {
  47. foreach (DataColumn dc in dt.Columns)
  48. {
  49. selectConetnt.Append(dc.Caption + " as " + dc.ColumnName + ",");
  50. }
  51. }
  52. return selectConetnt.Remove(selectConetnt.Length - 1, 1).ToString();
  53. }
  54. /// <summary>
  55. /// 禁止DataGirdView排序
  56. /// </summary>
  57. /// <param name="Dgv"></param>
  58. public static void DataGridViewNotSort(DataGridView Dgv)
  59. {
  60. foreach (DataGridViewColumn item in Dgv.Columns)
  61. item.SortMode = DataGridViewColumnSortMode.NotSortable;
  62. }
  63. public static string GetLocalIP()
  64. {
  65. try
  66. {
  67. string HostName = Dns.GetHostName(); //得到主机名
  68. IPHostEntry IpEntry = Dns.GetHostEntry(HostName);
  69. for (int i = 0; i < IpEntry.AddressList.Length; i++)
  70. {
  71. //从IP地址列表中筛选出IPv4类型的IP地址
  72. //AddressFamily.InterNetwork表示此IP为IPv4,
  73. //AddressFamily.InterNetworkV6表示此地址为IPv6类型
  74. if (IpEntry.AddressList[i].AddressFamily == AddressFamily.InterNetwork)
  75. {
  76. return IpEntry.AddressList[i].ToString();
  77. }
  78. }
  79. return "";
  80. }
  81. catch (Exception ex)
  82. {
  83. MessageBox.Show("获取本机IP出错:" + ex.Message);
  84. return "";
  85. }
  86. }
  87. /// <summary>
  88. /// 通过字段和其展示的中文值获取查询的内容
  89. /// </summary>
  90. /// <param name="field"></param>
  91. /// <param name="cnfield"></param>
  92. /// <returns></returns>
  93. public static string GetSelectContentByStringArray(string[] field, string[] cnfield)
  94. {
  95. StringBuilder sb = new StringBuilder();
  96. for (int i = 0; i < field.Length; i++)
  97. {
  98. sb.Append(field[i] + " as " + cnfield[i] + ",");
  99. }
  100. //去掉多余的逗号
  101. sb.Remove(sb.Length - 1, 1);
  102. return sb.ToString();
  103. }
  104. /// <summary>
  105. /// 传入控件的集合和DataTable,通过判断控件的名称和数据源的列的描述来匹配,支持单层的GroupBox和Panel
  106. /// </summary>
  107. /// <param name="collection"></param>
  108. /// <param name="dt"></param>
  109. public static void SetFormValue(ControlCollection collection, DataTable dt)
  110. {
  111. //DataTable存在数据才进行赋值操作
  112. if (dt.Rows.Count > 0)
  113. {
  114. for (int i = 0; i < collection.Count; i++)
  115. {
  116. //如果含有子控件则进行递归调用
  117. if (collection[i].Controls.Count > 0)
  118. SetFormValue(collection[i].Controls, dt);
  119. string controlName = collection[i].Name;
  120. string controlsTag = collection[i].Tag == null ? "" : collection[i].Tag.ToString();
  121. //默认给TextBox和Label赋值
  122. if (collection[i] is TextBox || collection[i] is Label || collection[i] is SearchTextBox || collection[i] is MaCodeSearchTextBox||collection[i] is EnterTextBox ||collection[i] is TextBoxGeneratePaCode)
  123. {
  124. for (int j = 0; j < dt.Columns.Count; j++)
  125. {
  126. if (controlName.ToUpper() == dt.Columns[j].Caption.ToUpper() || controlsTag.ToUpper() == dt.Columns[j].Caption.ToUpper())
  127. {
  128. //字段含有Status内容的才进行转换
  129. if (controlName.ToUpper().Contains("STATUS") || controlsTag.ToUpper().Contains("STATUS"))
  130. {
  131. //对审批状态进行判断
  132. switch (dt.Rows[0][j].ToString().ToUpper())
  133. {
  134. case "ENTERING":
  135. collection[i].Text = "在录入";
  136. break;
  137. case "UNAPPROVED":
  138. collection[i].Text = "未批准";
  139. break;
  140. case "COMMITED":
  141. collection[i].Text = "已提交";
  142. break;
  143. case "APPROVE":
  144. collection[i].Text = "已批准";
  145. break;
  146. case "AUDITED":
  147. collection[i].Text = "已审核";
  148. break;
  149. case "STARTED":
  150. collection[i].Text = "已下放";
  151. break;
  152. case "UNCHECK":
  153. collection[i].Text = "待检验";
  154. break;
  155. case "CHECKING":
  156. collection[i].Text = "检验中";
  157. break;
  158. default:
  159. collection[i].Text = dt.Rows[0][j].ToString();
  160. break;
  161. }
  162. }
  163. else
  164. collection[i].Text = dt.Rows[0][j].ToString();
  165. }
  166. }
  167. }
  168. //对封装在GroupBox的和Panel的控件进行批量赋值
  169. if (collection[i] is GroupBox || collection[i] is Panel || collection[i] is GroupBoxWithBorder)
  170. {
  171. for (int j = 0; j < collection[i].Controls.Count; j++)
  172. {
  173. controlName = collection[i].Controls[j].Name;
  174. if (collection[i].Controls[j] is TextBox || collection[i].Controls[j] is Label || collection[i].Controls[j] is SearchTextBox || collection[i].Controls[j] is MaCodeSearchTextBox)
  175. {
  176. for (int k = 0; k < dt.Columns.Count; k++)
  177. {
  178. if (controlName.ToUpper() == dt.Columns[k].Caption.ToUpper())
  179. {
  180. collection[i].Controls[j].Text = dt.Rows[0][k].ToString();
  181. }
  182. }
  183. }
  184. }
  185. }
  186. }
  187. }
  188. //如果没有记录的话,将dt中含有的列的对应值设置为空
  189. else
  190. {
  191. for (int i = 0; i < collection.Count; i++)
  192. {
  193. if (collection[i] is TextBox || collection[i] is Label || collection[i] is SearchTextBox)
  194. {
  195. for (int j = 0; j < dt.Columns.Count; j++)
  196. {
  197. if (collection[i].Name.ToUpper() == dt.Columns[j].Caption.ToUpper())
  198. {
  199. collection[i].Text = "";
  200. }
  201. }
  202. }
  203. }
  204. }
  205. }
  206. /// <summary>
  207. /// 获取打印标签
  208. /// </summary>
  209. /// <param name="labelName"></param>
  210. /// <param name="labelUrl"></param>
  211. /// <param name="indate"></param>
  212. public static void GetPrintLabel(string labelName, string labelUrl, string indate)
  213. {
  214. string LabelUrl = labelUrl;
  215. string LabelName = labelName;
  216. System.DateTime time = Convert.ToDateTime(indate);
  217. FileInfo file = new FileInfo(ftpOperater.DownLoadTo + LabelName);
  218. if (time.ToString() != file.LastWriteTime.ToString())
  219. BaseUtil.GetLabelUrl(LabelUrl, LabelName, time);
  220. }
  221. /// <summary>
  222. /// 获取标签的路径
  223. /// </summary>
  224. /// <param name="URL"></param>
  225. /// <param name="LabelName"></param>
  226. /// <param name="time"></param>
  227. /// <returns></returns>
  228. public static string GetLabelUrl(string URL, string LabelName, System.DateTime time)
  229. {
  230. //如果是传入的数据是从FTP取的文件
  231. if (URL.Contains("ftp:"))
  232. {
  233. ftpOperater ftp = new ftpOperater();
  234. return ftp.Download(LabelName);
  235. }
  236. else
  237. {
  238. return URL;
  239. }
  240. }
  241. /// <summary>
  242. /// 移除重复行
  243. /// </summary>
  244. /// <param name="dt"></param>
  245. /// <param name="field"></param>
  246. /// <returns></returns>
  247. public static DataTable DeleteSameRow(DataTable dt, string field)
  248. {
  249. ArrayList indexList = new ArrayList();
  250. // 找出待删除的行索引
  251. for (int i = 0; i < dt.Rows.Count - 1; i++)
  252. {
  253. if (!IsContain(indexList, i))
  254. {
  255. for (int j = i + 1; j < dt.Rows.Count; j++)
  256. {
  257. if (dt.Rows[i][field].ToString() == dt.Rows[j][field].ToString())
  258. {
  259. indexList.Add(j);
  260. }
  261. }
  262. }
  263. }
  264. indexList.Sort();
  265. // 排序
  266. for (int i = indexList.Count - 1; i >= 0; i--)// 根据待删除索引列表删除行
  267. {
  268. int index = Convert.ToInt32(indexList[i]);
  269. dt.Rows.RemoveAt(index);
  270. }
  271. return dt;
  272. }
  273. /// <summary>
  274. /// 判断数组中是否存在
  275. /// </summary>
  276. /// <param name="indexList">数组</param>
  277. /// <param name="index">索引</param>
  278. /// <returns></returns>
  279. public static bool IsContain(ArrayList indexList, int index)
  280. {
  281. for (int i = 0; i < indexList.Count; i++)
  282. {
  283. int tempIndex = Convert.ToInt32(indexList[i]);
  284. if (tempIndex == index)
  285. {
  286. return true;
  287. }
  288. }
  289. return false;
  290. }
  291. //播放音频文件
  292. public static void PlaySound(string FileName)
  293. {
  294. //要加载COM组件:Microsoft speech object Library
  295. if (!System.IO.File.Exists(FileName))
  296. {
  297. return;
  298. }
  299. SpeechLib.SpVoiceClass pp = new SpeechLib.SpVoiceClass();
  300. SpeechLib.SpFileStreamClass spFs = new SpeechLib.SpFileStreamClass();
  301. spFs.Open(FileName, SpeechLib.SpeechStreamFileMode.SSFMOpenForRead, true);
  302. SpeechLib.ISpeechBaseStream Istream = spFs as SpeechLib.ISpeechBaseStream;
  303. pp.SpeakStream(Istream, SpeechLib.SpeechVoiceSpeakFlags.SVSFIsFilename);
  304. spFs.Close();
  305. }
  306. /// <summary>
  307. /// 从DGV获取指定的列的数据形式是数组的形式
  308. /// </summary>
  309. public static ArrayList[] GetColumnDataFromDGV(DataGridView dgv, string[] ColumnName)
  310. {
  311. ArrayList[] array = new ArrayList[ColumnName.Length];
  312. //实例化和查询参数个数一样的ArrayList
  313. for (int i = 0; i < ColumnName.Length; i++)
  314. {
  315. array[i] = new ArrayList();
  316. }
  317. DataTable dt = (DataTable)dgv.DataSource;
  318. //如果第一列是否选框的话
  319. if (dgv.Columns[0] is DataGridViewCheckBoxColumn)
  320. {
  321. for (int i = 0; i < dt.Rows.Count; i++)
  322. {
  323. if (dgv.Rows[i].Cells[0].FormattedValue.ToString() == "True")
  324. {
  325. for (int j = 0; j < ColumnName.Length; j++)
  326. {
  327. array[j].Add(dt.Rows[i][ColumnName[j]]);
  328. }
  329. }
  330. }
  331. }
  332. //否则直接获取全部的数据
  333. else
  334. {
  335. for (int i = 0; i < dgv.RowCount; i++)
  336. {
  337. for (int j = 0; j < ColumnName.Length; j++)
  338. {
  339. array[j].Add(dt.Rows[i][ColumnName[j]]);
  340. }
  341. }
  342. }
  343. return array;
  344. }
  345. /// <summary>
  346. /// 通过DataGridView和需要隐藏的字段的数组来对字段进行隐藏
  347. /// </summary>
  348. /// <param name="dgv"></param>
  349. /// <param name="field"></param>
  350. public static void HideField(DataGridView dgv, string[] field)
  351. {
  352. DataTable dt = (DataTable)dgv.DataSource;
  353. foreach (DataColumn dc in dt.Columns)
  354. {
  355. foreach (string s in field)
  356. {
  357. if (dc.Caption == s)
  358. dgv.Columns[dc.ColumnName].Visible = false;
  359. }
  360. }
  361. }
  362. /// <summary>
  363. ///
  364. /// </summary>
  365. /// <param name="dgv"></param>
  366. public static void ExpandDGVCheck(DataGridViewExpand dgv, DataGridViewCellEventArgs e)
  367. {
  368. if (e.ColumnIndex == 0 && e.RowIndex >= 0)
  369. {
  370. if (dgv.Rows[e.RowIndex] is CollapseDataGridViewRow)
  371. {
  372. int CollapseRowCount = (dgv.Rows[e.RowIndex] as CollapseDataGridViewRow).Rows.Count;
  373. if (CollapseRowCount > 0)
  374. {
  375. for (int i = (e.RowIndex + 2); i < (e.RowIndex + 1 + CollapseRowCount); i++)
  376. {
  377. try
  378. {
  379. dgv.Rows[i].Cells[0].Value = dgv.Rows[e.RowIndex].Cells[0].EditedFormattedValue;
  380. }
  381. catch (Exception) { }
  382. }
  383. }
  384. }
  385. }
  386. }
  387. /// <summary>
  388. /// 通过查询的内容获取到字段的描述
  389. /// </summary>
  390. /// <param name="field"></param>
  391. /// <returns></returns>
  392. public static string[] GetCaptionFromField(string field)
  393. {
  394. string[] caption = field.Split(',');
  395. for (int i = 0; i < caption.Length; i++)
  396. {
  397. caption[i] = caption[i].Substring(0, caption[i].LastIndexOf("as")).Trim();
  398. }
  399. return caption;
  400. }
  401. /// <summary>
  402. /// 通过查询的语句获取查询的字段
  403. /// </summary>
  404. /// <param name="field"></param>
  405. /// <returns></returns>
  406. public static string[] GetField(string field)
  407. {
  408. string[] fields = field.Split(',');
  409. for (int i = 0; i < fields.Length; i++)
  410. {
  411. fields[i] = fields[i].Substring(fields[i].LastIndexOf("as") + 2, fields[i].Length - fields[i].LastIndexOf("as") - 2).Trim();
  412. }
  413. return fields;
  414. }
  415. /// <summary>
  416. /// 通过描述取DataTable的列名,主要用于从配置中取数据
  417. /// </summary>
  418. /// <param name="dt"></param>
  419. /// <param name="caption"></param>
  420. /// <returns></returns>
  421. public static string GetColumnNameByCaption(DataTable dt, string caption)
  422. {
  423. foreach (DataColumn dc in dt.Columns)
  424. {
  425. if (dc.Caption.ToLower() == caption)
  426. {
  427. return dc.ColumnName;
  428. }
  429. }
  430. return null;
  431. }
  432. //用于封装异常,也可以用于错误的提示
  433. public static void ShowError(string errorMessage)
  434. {
  435. throw new Exception(errorMessage);
  436. }
  437. /// <summary>
  438. /// 判断控件的某个事件是否已经绑定了方法
  439. /// </summary>
  440. /// <param name="Control1"></param>
  441. /// <param name="EventName"></param>
  442. /// <returns></returns>
  443. public static bool ControlHasEvent(Control Control1, string EventName)
  444. {
  445. //需要查询的内容
  446. BindingFlags myBindingFlags = BindingFlags.Static | BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic;
  447. Assembly a = Assembly.GetAssembly(Control1.GetType());
  448. Type t = a.GetType(Control1.GetType().FullName, true);
  449. //获取控件的事件
  450. FieldInfo fi = t.GetField(EventName, myBindingFlags);
  451. EventHandlerList ehl = Control1.GetType().GetProperty("Events", BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.FlattenHierarchy).GetValue(Control1, null) as EventHandlerList;
  452. //判断事件的委托数量是否大于0
  453. if (ehl != null)
  454. {
  455. Delegate d = ehl[fi.GetValue(Control1)];
  456. if (d != null && d.GetInvocationList().Length > 0)
  457. {
  458. return true;
  459. }
  460. }
  461. return false;
  462. }
  463. /// <summary>
  464. /// 获取控件的事件列表
  465. /// </summary>
  466. /// <param name="Control1"></param>
  467. /// <returns></returns>
  468. public static FieldInfo[] GetControlsEvent(Control Control1)
  469. {
  470. BindingFlags myBindingFlags = BindingFlags.Static | BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic;
  471. Assembly a = Assembly.GetAssembly(Control1.GetType());
  472. Type t = a.GetType(Control1.GetType().FullName, true);
  473. FieldInfo[] finf = t.GetFields(myBindingFlags);
  474. return finf;
  475. }
  476. /// <summary>
  477. /// 清除DataTable的结构和数据,清除列结构时需要从最后的一列开始删
  478. /// </summary>
  479. /// <param name="dt"></param>
  480. public static void CleanDataTable(DataTable dt)
  481. {
  482. for (int i = dt.Columns.Count - 1; i >= 0; i--)
  483. dt.Columns.Remove(dt.Columns[i]);
  484. }
  485. /// <summary>
  486. /// 获取标签的路径
  487. /// </summary>
  488. /// <param name="URL"></param>
  489. /// <param name="LabelName"></param>
  490. /// <returns></returns>
  491. public static string GetLabelUrl(string URL, string LabelName)
  492. {
  493. //如果是传入的数据是从FTP取的文件
  494. if (URL.Contains("ftp:"))
  495. {
  496. ftpOperater ftp = new ftpOperater();
  497. return ftp.Download(LabelName);
  498. }
  499. else
  500. return URL;
  501. }
  502. /// <summary>
  503. /// 往DataTable中添加数据
  504. /// </summary>
  505. public static void AddDataToDataTable(DataTable dt, params string[] param)
  506. {
  507. DataRow dr = dt.NewRow();
  508. for (int i = 0; i < dt.Columns.Count; i++)
  509. {
  510. dr[dt.Columns[i].ColumnName] = param[i];
  511. }
  512. }
  513. /// <summary>
  514. /// 不清除表结构,只清除数据
  515. /// </summary>
  516. /// <param name="dt"></param>
  517. public static void CleanDataTableData(DataTable dt)
  518. {
  519. for (int i = dt.Rows.Count - 1; i >= 0; i--)
  520. {
  521. dt.Rows.Remove(dt.Rows[i]);
  522. }
  523. }
  524. /// <summary>
  525. /// 获取拼接的字段
  526. /// </summary>
  527. /// <param name="dt"></param>
  528. /// <returns></returns>
  529. public static string GetFieldFromDataTable(DataTable dt)
  530. {
  531. StringBuilder sb = new StringBuilder();
  532. foreach (DataColumn dc in dt.Columns)
  533. {
  534. sb.Append(dc.Caption + ",");
  535. }
  536. return sb.ToString().Substring(sb.ToString().Length - 1, 1);
  537. }
  538. /// <summary>
  539. /// 已经定义好的DataGridView绑定数据,operate是用来添加操作列的
  540. /// </summary>
  541. /// <param name="dgv"></param>
  542. /// <param name="dt"></param>
  543. /// <param name="AddOpetateColumn"></param>
  544. /// <param name="operate"></param>
  545. public static void FillDgvWithDataTable(DataGridView dgv, DataTable dt, params DataGridViewImageColumn[] operate)
  546. {
  547. dgv.AutoGenerateColumns = false;
  548. dgv.DataSource = dt;
  549. if (operate.Length > 0)
  550. {
  551. if (dgv.Columns[operate[0].Name] != null)
  552. {
  553. dgv.Columns.Remove(dgv.Columns[operate[0].Name]);
  554. }
  555. dgv.Columns.Add(operate[0]);
  556. }
  557. ////纯英文的列不予展示
  558. //Regex regEnglish = new Regex("^[A-z]+$");
  559. //foreach (DataGridViewColumn dgvc in dgv.Columns)
  560. //{
  561. // if (regEnglish.IsMatch(dgvc.HeaderText))
  562. // {
  563. // dgvc.Visible = false;
  564. // }
  565. //}
  566. }
  567. /// <summary>
  568. /// 清除DataGridView的数据
  569. /// </summary>
  570. /// <param name="dgv"></param>
  571. public static void CleanDGVData(DataGridView dgv)
  572. {
  573. for (int i = dgv.Rows.Count - 1; i >= 0; i--)
  574. {
  575. dgv.Rows.RemoveAt(i);
  576. }
  577. }
  578. public static void CleanForm(Form Form)
  579. {
  580. for (int i = 0; i < Form.Controls.Count; i++)
  581. {
  582. if (Form.Controls[i] is EnterTextBox || Form.Controls[i] is TextBox || Form.Controls[i] is ValueLabel || Form.Controls[i] is SearchTextBox || Form.Controls[i] is ValueNumLabel || Form.Controls[i] is MaCodeSearchTextBox)
  583. Form.Controls[i].Text = "";
  584. if (Form.Controls[i] is DataGridView)
  585. CleanDGVData((DataGridView)Form.Controls[i]);
  586. }
  587. }
  588. public static void CleanControlsText(params Control[] ctl)
  589. {
  590. foreach (Control item in ctl)
  591. item.Text = "";
  592. }
  593. /// <summary>
  594. /// 需要SQL的顺序和DGV的列的顺序一致
  595. /// </summary>
  596. /// <param name="dgv"></param>
  597. /// <param name="dt"></param>
  598. /// <param name="CheckBox"></param>
  599. public static void FillExpandDgvWithDataTable(DataGridViewExpand dgv, DataTable dt, bool CheckBox, bool CheckBoxTrue)
  600. {
  601. CleanDGVData(dgv);
  602. if (CheckBox)
  603. {
  604. for (int i = 0; i < dt.Rows.Count; i++)
  605. {
  606. CollapseDataGridViewRow collapseRow = new CollapseDataGridViewRow();
  607. collapseRow.IsCollapse = true;
  608. DataGridViewCheckBoxCell checkcell = new DataGridViewCheckBoxCell();
  609. collapseRow.Cells.Add(checkcell);
  610. checkcell.Value = CheckBoxTrue;
  611. //因为DGV中可能有空置的列多出,所以需要用DataTable的列进行循环
  612. for (int j = 0; j < dt.Columns.Count; j++)
  613. {
  614. DataGridViewTextBoxCell textcell = new DataGridViewTextBoxCell();
  615. textcell.Value = dt.Rows[i][j].ToString();
  616. collapseRow.Cells.Add(textcell);
  617. textcell.ReadOnly = true;
  618. }
  619. collapseRow.Tag = "MainRow";
  620. dgv.Rows.Add(collapseRow);
  621. }
  622. }
  623. else
  624. {
  625. for (int i = 0; i < dt.Rows.Count; i++)
  626. {
  627. CollapseDataGridViewRow collapseRow = new CollapseDataGridViewRow();
  628. collapseRow.IsCollapse = true;
  629. for (int j = 1; j <= dt.Columns.Count; j++)
  630. {
  631. DataGridViewTextBoxCell textcell = new DataGridViewTextBoxCell();
  632. textcell.Value = dt.Rows[i][j - 1].ToString();
  633. collapseRow.Cells.Add(textcell);
  634. }
  635. dgv.Rows.Add(collapseRow);
  636. collapseRow.ReadOnly = true;
  637. }
  638. }
  639. }
  640. /// <summary>
  641. /// 用于给DGV中的Combox列赋静态值
  642. /// </summary>
  643. /// <param name="dgvc"></param>
  644. /// <param name="displayField"></param>
  645. /// <param name="valueField"></param>
  646. /// <param name="Value"></param>
  647. /// <returns></returns>
  648. public static void SetDgvColumnComboxData(DataGridViewComboBoxColumn dgvc, string DataPropertyName, string displayField, string valueField, string[] Value)
  649. {
  650. DataTable dt = new DataTable();
  651. dt.Columns.Add(displayField);
  652. dt.Columns.Add(valueField);
  653. for (int i = 0; i < Value.Length; i++)
  654. {
  655. DataGridViewRow row = new DataGridViewRow();
  656. dt.Rows.Add(row);
  657. dt.Rows[i][displayField] = Value[i].Split('#')[0];
  658. dt.Rows[i][valueField] = Value[i].Split('#')[1];
  659. }
  660. dgvc.DataPropertyName = DataPropertyName;
  661. dgvc.DataSource = dt;
  662. dgvc.DisplayMember = displayField;
  663. dgvc.ValueMember = valueField;
  664. }
  665. /// <summary>
  666. /// 用于给DGV中的ComboxCell赋静态值
  667. /// </summary>
  668. /// <param name="dgvcc"></param>
  669. /// <param name="displayField"></param>
  670. /// <param name="valueField"></param>
  671. /// <param name="Value"></param>
  672. public static void SetDGVCellComboxData(DataGridViewComboBoxCell dgvcc, string displayField, string valueField, string[] Value)
  673. {
  674. DataTable dt = new DataTable();
  675. dt.Columns.Add(displayField);
  676. dt.Columns.Add(valueField);
  677. for (int i = 0; i < Value.Length; i++)
  678. {
  679. DataRow dr = dt.NewRow();
  680. dr[displayField] = Value[i].Split('#')[0];
  681. dr[valueField] = Value[i].Split('#')[1];
  682. dt.Rows.Add(dr);
  683. }
  684. dgvcc.DisplayMember = displayField;
  685. dgvcc.ValueMember = valueField;
  686. dgvcc.DataSource = dt;
  687. }
  688. /// <summary>
  689. /// 获取刷选的SQL语句,传入的是TextBox的Control,传入的SQL不带Where条件
  690. /// </summary>
  691. /// <param name="SQL"></param>
  692. /// <param name="Condition"></param>
  693. /// <returns></returns>
  694. public static string GetScreenSqlCondition(params Control[] Condition)
  695. {
  696. string condition = "";
  697. //用于统计传入的控件的空值数
  698. int EmptyControlCount = 0;
  699. for (int i = 0; i < Condition.Length; i++)
  700. {
  701. //如果Text不为空再进行条件的拼接
  702. if (Condition[i].Text != "")
  703. {
  704. if (Condition[i] is ComboBox)
  705. {
  706. condition += "(" + Condition[i].Tag + " like " + "'%" + (Condition[i] as ComboBox).SelectedValue + "%' )";
  707. }
  708. else
  709. {
  710. condition += "(" + Condition[i].Tag + " like " + "'%" + Condition[i].Text + "%' )";
  711. }
  712. //如果不是最后要判断之后有没有空值的如果有一个Text的值不为空都需要添加and
  713. //添加了一次And之后跳出循环,因为如果后面多项有值会重复添加and
  714. for (int j = i + 1; j < Condition.Length; j++)
  715. {
  716. if (j < Condition.Length)
  717. {
  718. if (Condition[j].Text != "")
  719. {
  720. condition += " and ";
  721. break;
  722. }
  723. }
  724. }
  725. }
  726. else
  727. {
  728. EmptyControlCount = EmptyControlCount + 1;
  729. }
  730. }
  731. //如果所有的控件传入的都是空值则返回也为空
  732. if (EmptyControlCount == Condition.Length)
  733. return "";
  734. else
  735. condition = " where " + condition;
  736. return condition;
  737. }
  738. public static void CleanDataGridView(DataGridView dgv)
  739. {
  740. for (int i = dgv.Columns.Count - 1; i >= 0; i--)
  741. {
  742. dgv.Columns.RemoveAt(i);
  743. }
  744. }
  745. /// <summary>
  746. /// 取出SQL中的参数占位符
  747. /// </summary>
  748. /// <param name="SQL"></param>
  749. /// <returns></returns>
  750. public static string[] GetParamFromSQL(string SQL)
  751. {
  752. string[] par = SQL.Split(':');
  753. //用来存参数的数组
  754. StringBuilder[] addpar = new StringBuilder[par.Length - 1];
  755. string[] param = new string[par.Length - 1];
  756. for (int i = 0; i < par.Length - 1; i++)
  757. {
  758. //新建一个char类型的数组用来存储每个字节的变量
  759. char[] c = par[i + 1].ToCharArray();
  760. addpar[i] = new StringBuilder();
  761. for (int j = 0; j < c.Length; j++)
  762. {
  763. if (c[j] != ' ' && c[j] != ',' && c[j] != ')')
  764. {
  765. addpar[i].Append(c[j]);
  766. }
  767. else
  768. {
  769. break;
  770. }
  771. }
  772. }
  773. for (int i = 0; i < par.Length - 1; i++)
  774. {
  775. param[i] = addpar[i].ToString();
  776. }
  777. return param;
  778. }
  779. public static void SetFormCenter(Form form)
  780. {
  781. form.StartPosition = FormStartPosition.CenterParent;
  782. }
  783. /// <summary>
  784. /// 设置DataGridView的指定列可编辑
  785. /// </summary>
  786. /// <param name="DGV"></param>
  787. /// <param name="EditAbleField"></param>
  788. public static void SetDataGridViewReadOnly(DataGridView DGV, string[] EditAbleField)
  789. {
  790. foreach (DataGridViewColumn dc in DGV.Columns)
  791. {
  792. dc.ReadOnly = true;
  793. foreach (string s in EditAbleField)
  794. {
  795. if (dc.Name.ToLower() == s.ToLower())
  796. {
  797. DGV.Columns[dc.Name].ReadOnly = false;
  798. }
  799. }
  800. }
  801. }
  802. //判断带有CheckBox的DGV是否有项目勾选了
  803. public static DataTable DGVIfChecked(DataGridView dgv)
  804. {
  805. int CheckCount = 0;
  806. DataTable dt = new DataTable();
  807. //第一列是勾选框,排除在循环之外
  808. for (int i = 1; i < dgv.Columns.Count; i++)
  809. {
  810. dt.Columns.Add(dgv.Columns[i].Name);
  811. }
  812. for (int i = 0; i < dgv.RowCount; i++)
  813. {
  814. if (dgv.Rows[i].Cells[0].Value != null)
  815. {
  816. if (dgv.Rows[i].Cells[0].FormattedValue.ToString() == "True")
  817. {
  818. if (dgv.Rows[i].Tag.ToString() == "SonRow")
  819. {
  820. DataRow dr = dt.NewRow();
  821. for (int j = 1; j < dgv.ColumnCount; j++)
  822. {
  823. dr[dgv.Columns[j].Name] = dgv.Rows[i].Cells[j].FormattedValue;
  824. }
  825. dt.Rows.Add(dr);
  826. CheckCount++;
  827. }
  828. }
  829. }
  830. }
  831. //判断是否勾选了明细
  832. if (CheckCount == 0)
  833. return null;
  834. return dt;
  835. }
  836. public static void GetExpandDGVCheckedRow(DataGridView dgv, DataTable dt, int RowIndex, int DistinctColumnIndex)
  837. {
  838. //第一列是勾选框,排除在循环之外
  839. if (dt.Columns.Count == 0)
  840. {
  841. for (int i = 0; i < dgv.Columns.Count; i++)
  842. dt.Columns.Add(dgv.Columns[i].Name);
  843. }
  844. //是展开的子行的数据
  845. if (dgv.Rows[RowIndex].Tag != null && dgv.Rows[RowIndex].Tag.ToString() == "SonRow")
  846. {
  847. DataRow dr = dt.NewRow();
  848. DataRow[] datarow = (dt.Select(dgv.Columns[DistinctColumnIndex].Name + " ='" + dgv.Rows[RowIndex].Cells[DistinctColumnIndex].FormattedValue + "'"));
  849. //判断值是否存在,存在移除重新添加
  850. if (datarow.Length > 0)
  851. {
  852. dt.Rows.Remove(datarow[0]);
  853. }
  854. for (int j = 0; j < dgv.ColumnCount; j++)
  855. {
  856. dr[dgv.Columns[j].Name] = dgv.Rows[RowIndex].Cells[j].FormattedValue;
  857. }
  858. dt.Rows.Add(dr);
  859. }
  860. }
  861. /// <summary>
  862. /// 设置只允许输入数字
  863. /// </summary>
  864. /// <param name="sender"></param>
  865. /// <param name="e"></param>
  866. public static void NumOnly(object sender, KeyPressEventArgs e)
  867. {
  868. if (e.KeyChar != '\b')//这是允许输入退格键
  869. {
  870. if ((e.KeyChar < '0') || (e.KeyChar > '9'))//这是允许输入0-9数字
  871. {
  872. e.Handled = true;
  873. }
  874. }
  875. }
  876. public static string AddField(string[] Fields)
  877. {
  878. string sql = " ";
  879. foreach (string field in Fields)
  880. sql += field + ",";
  881. return sql.Substring(0, sql.Length - 1);
  882. }
  883. /// <summary>
  884. /// 筛选DataTable
  885. /// </summary>
  886. /// <param name="dt"></param>
  887. /// <param name="condition"></param>
  888. /// <returns></returns>
  889. public static DataTable filterDataTable(DataTable dt, String condition)
  890. {
  891. //获取筛选条件中的列名,值
  892. DataRow[] dataRows = dt.Select(condition);
  893. DataTable ndt = dt.Clone();
  894. for (int i = 0; i < dataRows.Length; i++)
  895. {
  896. ndt.Rows.Add(dataRows[i].ItemArray);
  897. }
  898. return ndt;
  899. }
  900. /// <summary>
  901. /// 图表绘制公共方法样本
  902. /// </summary>
  903. /// <param name="chart1"></param>
  904. /// <param name="_dt"></param>
  905. /// <param name="seriesChartType"></param>
  906. /// <param name="_title"></param>
  907. /// <param name="XValueMember"></param>
  908. /// <param name="YValueMembers"></param>
  909. /// <param name="Xname"></param>
  910. /// <param name="Yname"></param>
  911. public static void ViewChart(Chart chart1, DataTable _dt, SeriesChartType seriesChartType, string _title, string XValueMember, string YValueMembers, string Xname, string Yname)
  912. {
  913. chart1.Series[0].ChartType = seriesChartType;
  914. chart1.DataSource = _dt;
  915. chart1.Series[0].MarkerStyle = MarkerStyle.Circle;
  916. chart1.Series[0].MarkerSize = 8;
  917. chart1.Series[0].XValueMember = XValueMember;
  918. chart1.Series[0].YValueMembers = YValueMembers;
  919. chart1.Series[0].Label = "#VAL";
  920. chart1.Series[0].LabelToolTip = Xname + ": #VAL\r\n " + Yname + " #VALX";
  921. chart1.Series[0].BackSecondaryColor = Color.DarkCyan;
  922. chart1.Series[0].BorderColor = Color.DarkOliveGreen;
  923. chart1.Series[0].LabelBackColor = Color.Transparent;
  924. chart1.Series[0].LegendText = Yname;
  925. chart1.Legends[0].Title = _title;
  926. //chart1.ChartAreas[0].Area3DStyle.Enable3D = true;
  927. }
  928. /// <summary>
  929. /// 将新增打印进程信息写入静态文件
  930. /// </summary>
  931. /// <param name="lbl"></param>
  932. public static void WriteLbl(ApplicationClass lbl)
  933. {
  934. String str = SystemInf.ProcessesID + "|" + lbl.PID;
  935. string sysdisc = Environment.GetEnvironmentVariable("windir").Substring(0, 1);
  936. FileStream fs = new FileStream(Directory.GetCurrentDirectory() + @"\" + "lblprocess" + ".txt", FileMode.Append, FileAccess.Write);
  937. Console.WriteLine(Directory.GetCurrentDirectory() + @"\" + "lblprocess" + ".txt");
  938. StreamWriter sw = new StreamWriter(fs);
  939. sw.WriteLine(str, Encoding.UTF8);
  940. sw.Close();
  941. fs.Close();
  942. }
  943. }
  944. }