BaseUtil.cs 37 KB

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