BaseUtil.cs 49 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297
  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.Diagnostics;
  8. using System.Drawing;
  9. using System.IO;
  10. using System.Linq;
  11. using System.Net;
  12. using System.Net.Sockets;
  13. using System.Reflection;
  14. using System.Text;
  15. using System.Text.RegularExpressions;
  16. using System.Threading;
  17. using System.Windows.Forms;
  18. using System.Windows.Forms.DataVisualization.Charting;
  19. using System.Xml;
  20. using UAS_MES.CustomControl.DataGrid_View;
  21. using UAS_MES.CustomControl.GroupBoxWithBorder;
  22. using UAS_MES.CustomControl.TextBoxWithIcon;
  23. using UAS_MES.CustomControl.ValueLabel;
  24. using UAS_MES.Entity;
  25. using static System.Windows.Forms.Control;
  26. namespace UAS_MES.PublicMethod
  27. {
  28. class BaseUtil
  29. {
  30. /// <summary>
  31. /// 通过DataTable的ColumnName和Caption来拼接一条语句
  32. /// </summary>
  33. /// <param name=""></param>
  34. /// <returns></returns>
  35. public static string GetGridViewSelectContent(DataGridView d)
  36. {
  37. StringBuilder selectConetnt = new StringBuilder();
  38. DataTable dt = (DataTable)d.DataSource;
  39. if (dt == null)
  40. {
  41. foreach (DataGridViewColumn dc in d.Columns)
  42. {
  43. if (dc.DataPropertyName != "" && dc.DataPropertyName != null)
  44. {
  45. selectConetnt.Append(dc.Name + " as " + dc.Name + ",");
  46. }
  47. }
  48. }
  49. else
  50. {
  51. foreach (DataColumn dc in dt.Columns)
  52. {
  53. selectConetnt.Append(dc.Caption + " as " + dc.ColumnName + ",");
  54. }
  55. }
  56. return selectConetnt.Remove(selectConetnt.Length - 1, 1).ToString();
  57. }
  58. /// <summary>
  59. /// 禁止DataGirdView排序
  60. /// </summary>
  61. /// <param name="Dgv"></param>
  62. public static void DataGridViewNotSort(DataGridView Dgv)
  63. {
  64. foreach (DataGridViewColumn item in Dgv.Columns)
  65. item.SortMode = DataGridViewColumnSortMode.NotSortable;
  66. }
  67. public static string GetLocalIP()
  68. {
  69. try
  70. {
  71. string HostName = Dns.GetHostName(); //得到主机名
  72. IPHostEntry IpEntry = Dns.GetHostEntry(HostName);
  73. for (int i = 0; i < IpEntry.AddressList.Length; i++)
  74. {
  75. //从IP地址列表中筛选出IPv4类型的IP地址
  76. //AddressFamily.InterNetwork表示此IP为IPv4,
  77. //AddressFamily.InterNetworkV6表示此地址为IPv6类型
  78. if (IpEntry.AddressList[i].AddressFamily == AddressFamily.InterNetwork)
  79. {
  80. return IpEntry.AddressList[i].ToString();
  81. }
  82. }
  83. return "";
  84. }
  85. catch (Exception ex)
  86. {
  87. MessageBox.Show("获取本机IP出错:" + ex.Message);
  88. return "";
  89. }
  90. }
  91. /// <summary>
  92. /// 通过字段和其展示的中文值获取查询的内容
  93. /// </summary>
  94. /// <param name="field"></param>
  95. /// <param name="cnfield"></param>
  96. /// <returns></returns>
  97. public static string GetSelectContentByStringArray(string[] field, string[] cnfield)
  98. {
  99. StringBuilder sb = new StringBuilder();
  100. for (int i = 0; i < field.Length; i++)
  101. {
  102. sb.Append(field[i] + " as " + cnfield[i] + ",");
  103. }
  104. //去掉多余的逗号
  105. sb.Remove(sb.Length - 1, 1);
  106. return sb.ToString();
  107. }
  108. /// <summary>
  109. /// 传入控件的集合和DataTable,通过判断控件的名称和数据源的列的描述来匹配,支持单层的GroupBox和Panel
  110. /// </summary>
  111. /// <param name="collection"></param>
  112. /// <param name="dt"></param>
  113. public static void SetFormValue(ControlCollection collection, DataTable dt)
  114. {
  115. //DataTable存在数据才进行赋值操作
  116. if (dt.Rows.Count > 0)
  117. {
  118. for (int i = 0; i < collection.Count; i++)
  119. {
  120. //如果含有子控件则进行递归调用
  121. if (collection[i].Controls.Count > 0)
  122. SetFormValue(collection[i].Controls, dt);
  123. string controlName = collection[i].Name;
  124. string controlsTag = collection[i].Tag == null ? "" : collection[i].Tag.ToString();
  125. //默认给TextBox和Label赋值
  126. 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 || collection[i] is NumericUpDown||collection[i] is TextBoxWithTextArea)
  127. {
  128. for (int j = 0; j < dt.Columns.Count; j++)
  129. {
  130. if (controlName.ToUpper() == dt.Columns[j].Caption.ToUpper() || controlsTag.ToUpper() == dt.Columns[j].Caption.ToUpper())
  131. {
  132. //字段含有Status内容的才进行转换
  133. if (controlName.ToUpper().Contains("STATUS") || controlsTag.ToUpper().Contains("STATUS"))
  134. {
  135. //对审批状态进行判断
  136. switch (dt.Rows[0][j].ToString().ToUpper())
  137. {
  138. case "ENTERING":
  139. collection[i].Text = "在录入";
  140. break;
  141. case "UNAPPROVED":
  142. collection[i].Text = "未批准";
  143. break;
  144. case "COMMITED":
  145. collection[i].Text = "已提交";
  146. break;
  147. case "APPROVE":
  148. collection[i].Text = "已批准";
  149. break;
  150. case "AUDITED":
  151. collection[i].Text = "已审核";
  152. break;
  153. case "STARTED":
  154. collection[i].Text = "已下放";
  155. break;
  156. case "UNCHECK":
  157. collection[i].Text = "待检验";
  158. break;
  159. case "CHECKING":
  160. collection[i].Text = "检验中";
  161. break;
  162. default:
  163. collection[i].Text = dt.Rows[0][j].ToString();
  164. break;
  165. }
  166. }
  167. else
  168. collection[i].Text = dt.Rows[0][j].ToString();
  169. }
  170. }
  171. }
  172. //对封装在GroupBox的和Panel的控件进行批量赋值
  173. if (collection[i] is GroupBox || collection[i] is Panel || collection[i] is GroupBoxWithBorder)
  174. {
  175. for (int j = 0; j < collection[i].Controls.Count; j++)
  176. {
  177. controlName = collection[i].Controls[j].Name;
  178. 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)
  179. {
  180. for (int k = 0; k < dt.Columns.Count; k++)
  181. {
  182. if (controlName.ToUpper() == dt.Columns[k].Caption.ToUpper())
  183. {
  184. collection[i].Controls[j].Text = dt.Rows[0][k].ToString();
  185. }
  186. }
  187. }
  188. }
  189. }
  190. }
  191. }
  192. //如果没有记录的话,将dt中含有的列的对应值设置为空
  193. else
  194. {
  195. for (int i = 0; i < collection.Count; i++)
  196. {
  197. if (collection[i] is TextBox || collection[i] is Label || collection[i] is SearchTextBox)
  198. {
  199. for (int j = 0; j < dt.Columns.Count; j++)
  200. {
  201. if (collection[i].Name.ToUpper() == dt.Columns[j].Caption.ToUpper())
  202. {
  203. collection[i].Text = "";
  204. }
  205. }
  206. }
  207. }
  208. }
  209. }
  210. /// <summary>
  211. /// 获取打印标签
  212. /// </summary>
  213. /// <param name="labelName"></param>
  214. /// <param name="labelUrl"></param>
  215. /// <param name="indate"></param>
  216. public static void GetPrintLabel(string labelName, string labelUrl)
  217. {
  218. BaseUtil.GetLabelUrl(labelUrl, labelName);
  219. }
  220. /// <summary>
  221. /// 获取打印标签
  222. /// </summary>
  223. /// <param name="labelName"></param>
  224. /// <param name="labelUrl"></param>
  225. /// <param name="indate"></param>
  226. public static void GetPrintLabel(string labelName, string labelUrl, string indate)
  227. {
  228. string LabelUrl = labelUrl;
  229. string LabelName = labelName;
  230. System.DateTime time = Convert.ToDateTime(indate);
  231. FileInfo file = new FileInfo(ftpOperater.DownLoadTo + LabelName);
  232. if (time.ToString() != file.LastWriteTime.ToString())
  233. BaseUtil.GetLabelUrl(LabelUrl, LabelName, time);
  234. }
  235. /// <summary>
  236. /// 获取标签的路径
  237. /// </summary>
  238. /// <param name="URL"></param>
  239. /// <param name="LabelName"></param>
  240. /// <param name="time"></param>
  241. /// <returns></returns>
  242. public static string GetLabelUrl(string URL, string LabelName, System.DateTime time)
  243. {
  244. ftpOperater ftp = new ftpOperater();
  245. return ftp.DownLoadFromSharePath(URL, LabelName);
  246. }
  247. /// <summary>
  248. /// 移除重复行
  249. /// </summary>
  250. /// <param name="dt"></param>
  251. /// <param name="field"></param>
  252. /// <returns></returns>
  253. public static DataTable DeleteSameRow(DataTable dt, string field)
  254. {
  255. ArrayList indexList = new ArrayList();
  256. // 找出待删除的行索引
  257. for (int i = 0; i < dt.Rows.Count - 1; i++)
  258. {
  259. if (!IsContain(indexList, i))
  260. {
  261. for (int j = i + 1; j < dt.Rows.Count; j++)
  262. {
  263. if (dt.Rows[i][field].ToString() == dt.Rows[j][field].ToString())
  264. {
  265. indexList.Add(j);
  266. }
  267. }
  268. }
  269. }
  270. indexList.Sort();
  271. // 排序
  272. for (int i = indexList.Count - 1; i >= 0; i--)// 根据待删除索引列表删除行
  273. {
  274. int index = Convert.ToInt32(indexList[i]);
  275. dt.Rows.RemoveAt(index);
  276. }
  277. return dt;
  278. }
  279. /// <summary>
  280. /// 判断数组中是否存在
  281. /// </summary>
  282. /// <param name="indexList">数组</param>
  283. /// <param name="index">索引</param>
  284. /// <returns></returns>
  285. public static bool IsContain(ArrayList indexList, int index)
  286. {
  287. for (int i = 0; i < indexList.Count; i++)
  288. {
  289. int tempIndex = Convert.ToInt32(indexList[i]);
  290. if (tempIndex == index)
  291. {
  292. return true;
  293. }
  294. }
  295. return false;
  296. }
  297. //播放音频文件
  298. public static void PlaySound(string FileName)
  299. {
  300. //要加载COM组件:Microsoft speech object Library
  301. if (!System.IO.File.Exists(FileName))
  302. {
  303. return;
  304. }
  305. SpeechLib.SpVoiceClass pp = new SpeechLib.SpVoiceClass();
  306. SpeechLib.SpFileStreamClass spFs = new SpeechLib.SpFileStreamClass();
  307. spFs.Open(FileName, SpeechLib.SpeechStreamFileMode.SSFMOpenForRead, true);
  308. SpeechLib.ISpeechBaseStream Istream = spFs as SpeechLib.ISpeechBaseStream;
  309. pp.SpeakStream(Istream, SpeechLib.SpeechVoiceSpeakFlags.SVSFIsFilename);
  310. spFs.Close();
  311. }
  312. /// <summary>
  313. /// 从DGV获取指定的列的数据形式是数组的形式
  314. /// </summary>
  315. public static ArrayList[] GetColumnDataFromDGV(DataGridView dgv, string[] ColumnName)
  316. {
  317. ArrayList[] array = new ArrayList[ColumnName.Length];
  318. //实例化和查询参数个数一样的ArrayList
  319. for (int i = 0; i < ColumnName.Length; i++)
  320. {
  321. array[i] = new ArrayList();
  322. }
  323. DataTable dt = (DataTable)dgv.DataSource;
  324. //如果第一列是否选框的话
  325. if (dgv.Columns[0] is DataGridViewCheckBoxColumn)
  326. {
  327. for (int i = 0; i < dt.Rows.Count; i++)
  328. {
  329. if (dgv.Rows[i].Cells[0].FormattedValue.ToString() == "True")
  330. {
  331. for (int j = 0; j < ColumnName.Length; j++)
  332. {
  333. array[j].Add(dt.Rows[i][ColumnName[j]]);
  334. }
  335. }
  336. }
  337. }
  338. //否则直接获取全部的数据
  339. else
  340. {
  341. for (int i = 0; i < dgv.RowCount; i++)
  342. {
  343. for (int j = 0; j < ColumnName.Length; j++)
  344. {
  345. array[j].Add(dt.Rows[i][ColumnName[j]]);
  346. }
  347. }
  348. }
  349. return array;
  350. }
  351. /// <summary>
  352. /// 通过DataGridView和需要隐藏的字段的数组来对字段进行隐藏
  353. /// </summary>
  354. /// <param name="dgv"></param>
  355. /// <param name="field"></param>
  356. public static void HideField(DataGridView dgv, string[] field)
  357. {
  358. DataTable dt = (DataTable)dgv.DataSource;
  359. foreach (DataColumn dc in dt.Columns)
  360. {
  361. foreach (string s in field)
  362. {
  363. if (dc.Caption == s)
  364. dgv.Columns[dc.ColumnName].Visible = false;
  365. }
  366. }
  367. }
  368. /// <summary>
  369. ///
  370. /// </summary>
  371. /// <param name="dgv"></param>
  372. public static void ExpandDGVCheck(DataGridViewExpand dgv, DataGridViewCellEventArgs e)
  373. {
  374. if (e.ColumnIndex == 0 && e.RowIndex >= 0)
  375. {
  376. if (dgv.Rows[e.RowIndex] is CollapseDataGridViewRow)
  377. {
  378. int CollapseRowCount = (dgv.Rows[e.RowIndex] as CollapseDataGridViewRow).Rows.Count;
  379. if (CollapseRowCount > 0)
  380. {
  381. for (int i = (e.RowIndex + 2); i < (e.RowIndex + 1 + CollapseRowCount); i++)
  382. {
  383. try
  384. {
  385. dgv.Rows[i].Cells[0].Value = dgv.Rows[e.RowIndex].Cells[0].EditedFormattedValue;
  386. }
  387. catch (Exception) { }
  388. }
  389. }
  390. }
  391. }
  392. }
  393. /// <summary>
  394. /// 通过查询的内容获取到字段的描述
  395. /// </summary>
  396. /// <param name="field"></param>
  397. /// <returns></returns>
  398. public static string[] GetCaptionFromField(string field)
  399. {
  400. string[] caption = field.Split(',');
  401. for (int i = 0; i < caption.Length; i++)
  402. {
  403. caption[i] = caption[i].Substring(0, caption[i].LastIndexOf("as")).Trim();
  404. }
  405. return caption;
  406. }
  407. /// <summary>
  408. /// 通过查询的语句获取查询的字段
  409. /// </summary>
  410. /// <param name="field"></param>
  411. /// <returns></returns>
  412. public static string[] GetField(string field)
  413. {
  414. string[] fields = field.Split(',');
  415. for (int i = 0; i < fields.Length; i++)
  416. {
  417. fields[i] = fields[i].Substring(fields[i].LastIndexOf("as") + 2, fields[i].Length - fields[i].LastIndexOf("as") - 2).Trim();
  418. }
  419. return fields;
  420. }
  421. /// <summary>
  422. /// 通过描述取DataTable的列名,主要用于从配置中取数据
  423. /// </summary>
  424. /// <param name="dt"></param>
  425. /// <param name="caption"></param>
  426. /// <returns></returns>
  427. public static string GetColumnNameByCaption(DataTable dt, string caption)
  428. {
  429. foreach (DataColumn dc in dt.Columns)
  430. {
  431. if (dc.Caption.ToLower() == caption)
  432. {
  433. return dc.ColumnName;
  434. }
  435. }
  436. return null;
  437. }
  438. //用于封装异常,也可以用于错误的提示
  439. public static void ShowError(string errorMessage)
  440. {
  441. throw new Exception(errorMessage);
  442. }
  443. /// <summary>
  444. /// 判断控件的某个事件是否已经绑定了方法
  445. /// </summary>
  446. /// <param name="Control1"></param>
  447. /// <param name="EventName"></param>
  448. /// <returns></returns>
  449. public static bool ControlHasEvent(Control Control1, string EventName)
  450. {
  451. //需要查询的内容
  452. BindingFlags myBindingFlags = BindingFlags.Static | BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic;
  453. Assembly a = Assembly.GetAssembly(Control1.GetType());
  454. Type t = a.GetType(Control1.GetType().FullName, true);
  455. //获取控件的事件
  456. FieldInfo fi = t.GetField(EventName, myBindingFlags);
  457. EventHandlerList ehl = Control1.GetType().GetProperty("Events", BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.FlattenHierarchy).GetValue(Control1, null) as EventHandlerList;
  458. //判断事件的委托数量是否大于0
  459. if (ehl != null)
  460. {
  461. Delegate d = ehl[fi.GetValue(Control1)];
  462. if (d != null && d.GetInvocationList().Length > 0)
  463. {
  464. return true;
  465. }
  466. }
  467. return false;
  468. }
  469. /// <summary>
  470. /// 获取控件的事件列表
  471. /// </summary>
  472. /// <param name="Control1"></param>
  473. /// <returns></returns>
  474. public static FieldInfo[] GetControlsEvent(Control Control1)
  475. {
  476. BindingFlags myBindingFlags = BindingFlags.Static | BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic;
  477. Assembly a = Assembly.GetAssembly(Control1.GetType());
  478. Type t = a.GetType(Control1.GetType().FullName, true);
  479. FieldInfo[] finf = t.GetFields(myBindingFlags);
  480. return finf;
  481. }
  482. /// <summary>
  483. /// 清除DataTable的结构和数据,清除列结构时需要从最后的一列开始删
  484. /// </summary>
  485. /// <param name="dt"></param>
  486. public static void CleanDataTable(DataTable dt)
  487. {
  488. for (int i = dt.Columns.Count - 1; i >= 0; i--)
  489. dt.Columns.Remove(dt.Columns[i]);
  490. }
  491. /// <summary>
  492. /// 获取标签的路径
  493. /// </summary>
  494. /// <param name="URL"></param>
  495. /// <param name="LabelName"></param>
  496. /// <returns></returns>
  497. public static string GetLabelUrl(string URL, string LabelName)
  498. {
  499. ftpOperater ftp = new ftpOperater();
  500. return ftp.DownLoadFromSharePath(URL, LabelName);
  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].Controls.Count > 0)
  583. {
  584. CleanControls(Form.Controls[i].Controls);
  585. }
  586. 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)
  587. Form.Controls[i].Text = "";
  588. if (Form.Controls[i] is DataGridView)
  589. CleanDGVData((DataGridView)Form.Controls[i]);
  590. }
  591. }
  592. public static void CleanControls(ControlCollection collection)
  593. {
  594. for (int i = 0; i < collection.Count; i++)
  595. {
  596. if (collection[i].Controls.Count > 0)
  597. CleanControls(collection[i].Controls);
  598. if (collection[i] is EnterTextBox || collection[i] is TextBox || collection[i] is ValueLabel || collection[i] is SearchTextBox || collection[i] is ValueNumLabel || collection[i] is MaCodeSearchTextBox)
  599. collection[i].Text = "";
  600. if (collection[i] is DataGridView)
  601. CleanDGVData((DataGridView)collection[i]);
  602. }
  603. }
  604. public static void CleanControlsText(params Control[] ctl)
  605. {
  606. foreach (Control item in ctl)
  607. item.Text = "";
  608. }
  609. /// <summary>
  610. /// 需要SQL的顺序和DGV的列的顺序一致
  611. /// </summary>
  612. /// <param name="dgv"></param>
  613. /// <param name="dt"></param>
  614. /// <param name="CheckBox"></param>
  615. public static void FillExpandDgvWithDataTable(DataGridViewExpand dgv, DataTable dt, bool CheckBox, bool CheckBoxTrue)
  616. {
  617. CleanDGVData(dgv);
  618. if (CheckBox)
  619. {
  620. for (int i = 0; i < dt.Rows.Count; i++)
  621. {
  622. CollapseDataGridViewRow collapseRow = new CollapseDataGridViewRow();
  623. collapseRow.IsCollapse = true;
  624. DataGridViewCheckBoxCell checkcell = new DataGridViewCheckBoxCell();
  625. collapseRow.Cells.Add(checkcell);
  626. checkcell.Value = CheckBoxTrue;
  627. //因为DGV中可能有空置的列多出,所以需要用DataTable的列进行循环
  628. for (int j = 0; j < dt.Columns.Count; j++)
  629. {
  630. DataGridViewTextBoxCell textcell = new DataGridViewTextBoxCell();
  631. textcell.Value = dt.Rows[i][j].ToString();
  632. collapseRow.Cells.Add(textcell);
  633. textcell.ReadOnly = true;
  634. }
  635. collapseRow.Tag = "MainRow";
  636. dgv.Rows.Add(collapseRow);
  637. }
  638. }
  639. else
  640. {
  641. for (int i = 0; i < dt.Rows.Count; i++)
  642. {
  643. CollapseDataGridViewRow collapseRow = new CollapseDataGridViewRow();
  644. collapseRow.IsCollapse = true;
  645. for (int j = 1; j <= dt.Columns.Count; j++)
  646. {
  647. DataGridViewTextBoxCell textcell = new DataGridViewTextBoxCell();
  648. textcell.Value = dt.Rows[i][j - 1].ToString();
  649. collapseRow.Cells.Add(textcell);
  650. }
  651. dgv.Rows.Add(collapseRow);
  652. collapseRow.ReadOnly = true;
  653. }
  654. }
  655. }
  656. /// <summary>
  657. /// 用于给DGV中的Combox列赋静态值
  658. /// </summary>
  659. /// <param name="dgvc"></param>
  660. /// <param name="displayField"></param>
  661. /// <param name="valueField"></param>
  662. /// <param name="Value"></param>
  663. /// <returns></returns>
  664. public static void SetDgvColumnComboxData(DataGridViewComboBoxColumn dgvc, string DataPropertyName, string displayField, string valueField, string[] Value)
  665. {
  666. DataTable dt = new DataTable();
  667. dt.Columns.Add(displayField);
  668. dt.Columns.Add(valueField);
  669. for (int i = 0; i < Value.Length; i++)
  670. {
  671. DataGridViewRow row = new DataGridViewRow();
  672. dt.Rows.Add(row);
  673. dt.Rows[i][displayField] = Value[i].Split('#')[0];
  674. dt.Rows[i][valueField] = Value[i].Split('#')[1];
  675. }
  676. dgvc.DataPropertyName = DataPropertyName;
  677. dgvc.DataSource = dt;
  678. dgvc.DisplayMember = displayField;
  679. dgvc.ValueMember = valueField;
  680. }
  681. /// <summary>
  682. /// 用于给DGV中的ComboxCell赋静态值
  683. /// </summary>
  684. /// <param name="dgvcc"></param>
  685. /// <param name="displayField"></param>
  686. /// <param name="valueField"></param>
  687. /// <param name="Value"></param>
  688. public static void SetDGVCellComboxData(DataGridViewComboBoxCell dgvcc, string displayField, string valueField, string[] Value)
  689. {
  690. DataTable dt = new DataTable();
  691. dt.Columns.Add(displayField);
  692. dt.Columns.Add(valueField);
  693. for (int i = 0; i < Value.Length; i++)
  694. {
  695. DataRow dr = dt.NewRow();
  696. dr[displayField] = Value[i].Split('#')[0];
  697. dr[valueField] = Value[i].Split('#')[1];
  698. dt.Rows.Add(dr);
  699. }
  700. dgvcc.DisplayMember = displayField;
  701. dgvcc.ValueMember = valueField;
  702. dgvcc.DataSource = dt;
  703. }
  704. /// <summary>
  705. /// 获取刷选的SQL语句,传入的是TextBox的Control,传入的SQL不带Where条件
  706. /// </summary>
  707. /// <param name="SQL"></param>
  708. /// <param name="Condition"></param>
  709. /// <returns></returns>
  710. public static string GetScreenSqlCondition(params Control[] Condition)
  711. {
  712. string condition = "";
  713. //用于统计传入的控件的空值数
  714. int EmptyControlCount = 0;
  715. for (int i = 0; i < Condition.Length; i++)
  716. {
  717. //如果Text不为空再进行条件的拼接
  718. if (Condition[i].Text != "")
  719. {
  720. if (Condition[i] is ComboBox)
  721. {
  722. condition += "(" + Condition[i].Tag + " like " + "'%" + (Condition[i] as ComboBox).SelectedValue + "%' )";
  723. }
  724. else
  725. {
  726. condition += "(" + Condition[i].Tag + " like " + "'%" + Condition[i].Text + "%' )";
  727. }
  728. //如果不是最后要判断之后有没有空值的如果有一个Text的值不为空都需要添加and
  729. //添加了一次And之后跳出循环,因为如果后面多项有值会重复添加and
  730. for (int j = i + 1; j < Condition.Length; j++)
  731. {
  732. if (j < Condition.Length)
  733. {
  734. if (Condition[j].Text != "")
  735. {
  736. condition += " and ";
  737. break;
  738. }
  739. }
  740. }
  741. }
  742. else
  743. {
  744. EmptyControlCount = EmptyControlCount + 1;
  745. }
  746. }
  747. //如果所有的控件传入的都是空值则返回也为空
  748. if (EmptyControlCount == Condition.Length)
  749. return "";
  750. else
  751. condition = " where " + condition;
  752. return condition;
  753. }
  754. public static void CleanDataGridView(DataGridView dgv)
  755. {
  756. for (int i = dgv.Columns.Count - 1; i >= 0; i--)
  757. {
  758. dgv.Columns.RemoveAt(i);
  759. }
  760. }
  761. /// <summary>
  762. /// 取出SQL中的参数占位符
  763. /// </summary>
  764. /// <param name="SQL"></param>
  765. /// <returns></returns>
  766. public static string[] GetParamFromSQL(string SQL)
  767. {
  768. string[] par = SQL.Split(':');
  769. //用来存参数的数组
  770. StringBuilder[] addpar = new StringBuilder[par.Length - 1];
  771. string[] param = new string[par.Length - 1];
  772. for (int i = 0; i < par.Length - 1; i++)
  773. {
  774. //新建一个char类型的数组用来存储每个字节的变量
  775. char[] c = par[i + 1].ToCharArray();
  776. addpar[i] = new StringBuilder();
  777. for (int j = 0; j < c.Length; j++)
  778. {
  779. if (c[j] != ' ' && c[j] != ',' && c[j] != ')')
  780. {
  781. addpar[i].Append(c[j]);
  782. }
  783. else
  784. {
  785. break;
  786. }
  787. }
  788. }
  789. for (int i = 0; i < par.Length - 1; i++)
  790. {
  791. param[i] = addpar[i].ToString();
  792. }
  793. return param;
  794. }
  795. public static void SetFormCenter(Form form)
  796. {
  797. form.StartPosition = FormStartPosition.CenterParent;
  798. }
  799. /// <summary>
  800. /// 设置DataGridView的指定列可编辑
  801. /// </summary>
  802. /// <param name="DGV"></param>
  803. /// <param name="EditAbleField"></param>
  804. public static void SetDataGridViewReadOnly(DataGridView DGV, string[] EditAbleField)
  805. {
  806. foreach (DataGridViewColumn dc in DGV.Columns)
  807. {
  808. dc.ReadOnly = true;
  809. foreach (string s in EditAbleField)
  810. {
  811. if (dc.Name.ToLower() == s.ToLower())
  812. {
  813. DGV.Columns[dc.Name].ReadOnly = false;
  814. }
  815. }
  816. }
  817. }
  818. //判断带有CheckBox的DGV是否有项目勾选了
  819. public static DataTable DGVIfChecked(DataGridView dgv)
  820. {
  821. int CheckCount = 0;
  822. DataTable dt = new DataTable();
  823. //第一列是勾选框,排除在循环之外
  824. for (int i = 1; i < dgv.Columns.Count; i++)
  825. {
  826. dt.Columns.Add(dgv.Columns[i].Name);
  827. }
  828. for (int i = 0; i < dgv.RowCount; i++)
  829. {
  830. if (dgv.Rows[i].Cells[0].Value != null)
  831. {
  832. if (dgv.Rows[i].Cells[0].FormattedValue.ToString() == "True")
  833. {
  834. if (dgv.Rows[i].Tag.ToString() == "SonRow")
  835. {
  836. DataRow dr = dt.NewRow();
  837. for (int j = 1; j < dgv.ColumnCount; j++)
  838. {
  839. dr[dgv.Columns[j].Name] = dgv.Rows[i].Cells[j].FormattedValue;
  840. }
  841. dt.Rows.Add(dr);
  842. CheckCount++;
  843. }
  844. }
  845. }
  846. }
  847. //判断是否勾选了明细
  848. if (CheckCount == 0)
  849. return null;
  850. return dt;
  851. }
  852. public static void GetExpandDGVCheckedRow(DataGridView dgv, DataTable dt, int RowIndex, int DistinctColumnIndex)
  853. {
  854. //第一列是勾选框,排除在循环之外
  855. if (dt.Columns.Count == 0)
  856. {
  857. for (int i = 0; i < dgv.Columns.Count; i++)
  858. dt.Columns.Add(dgv.Columns[i].Name);
  859. }
  860. //是展开的子行的数据
  861. if (dgv.Rows[RowIndex].Tag != null && dgv.Rows[RowIndex].Tag.ToString() == "SonRow")
  862. {
  863. DataRow dr = dt.NewRow();
  864. DataRow[] datarow = (dt.Select(dgv.Columns[DistinctColumnIndex].Name + " ='" + dgv.Rows[RowIndex].Cells[DistinctColumnIndex].FormattedValue + "'"));
  865. //判断值是否存在,存在移除重新添加
  866. if (datarow.Length > 0)
  867. {
  868. dt.Rows.Remove(datarow[0]);
  869. }
  870. for (int j = 0; j < dgv.ColumnCount; j++)
  871. {
  872. dr[dgv.Columns[j].Name] = dgv.Rows[RowIndex].Cells[j].FormattedValue;
  873. }
  874. dt.Rows.Add(dr);
  875. }
  876. }
  877. /// <summary>
  878. /// 设置只允许输入数字
  879. /// </summary>
  880. /// <param name="sender"></param>
  881. /// <param name="e"></param>
  882. public static void NumOnly(object sender, KeyPressEventArgs e)
  883. {
  884. if (e.KeyChar != '\b')//这是允许输入退格键
  885. {
  886. if ((e.KeyChar < '0') || (e.KeyChar > '9'))//这是允许输入0-9数字
  887. {
  888. e.Handled = true;
  889. }
  890. }
  891. }
  892. public static string AddField(string[] Fields)
  893. {
  894. string sql = " ";
  895. foreach (string field in Fields)
  896. sql += field + ",";
  897. return sql.Substring(0, sql.Length - 1);
  898. }
  899. /// <summary>
  900. /// 筛选DataTable
  901. /// </summary>
  902. /// <param name="dt"></param>
  903. /// <param name="condition"></param>
  904. /// <returns></returns>
  905. public static DataTable filterDataTable(DataTable dt, String condition)
  906. {
  907. if (dt == null)
  908. return new DataTable();
  909. //获取筛选条件中的列名,值
  910. DataRow[] dataRows = dt.Select(condition);
  911. DataTable ndt = dt.Clone();
  912. for (int i = 0; i < dataRows.Length; i++)
  913. {
  914. ndt.Rows.Add(dataRows[i].ItemArray);
  915. }
  916. return ndt;
  917. }
  918. /// <summary>
  919. /// 图表绘制公共方法样本
  920. /// </summary>
  921. /// <param name="chart1"></param>
  922. /// <param name="_dt"></param>
  923. /// <param name="seriesChartType"></param>
  924. /// <param name="_title"></param>
  925. /// <param name="XValueMember"></param>
  926. /// <param name="YValueMembers"></param>
  927. /// <param name="Xname"></param>
  928. /// <param name="Yname"></param>
  929. public static void ViewChart(Chart chart1, DataTable _dt, SeriesChartType seriesChartType, string _title, string XValueMember, string YValueMembers, string Xname, string Yname)
  930. {
  931. chart1.Series[0].ChartType = seriesChartType;
  932. chart1.DataSource = _dt;
  933. chart1.Series[0].MarkerStyle = MarkerStyle.Circle;
  934. chart1.Series[0].MarkerSize = 8;
  935. chart1.Series[0].XValueMember = XValueMember;
  936. chart1.Series[0].YValueMembers = YValueMembers;
  937. chart1.Series[0].Label = "#VAL";
  938. chart1.Series[0].LabelToolTip = Xname + ": #VAL\r\n " + Yname + " #VALX";
  939. chart1.Series[0].BackSecondaryColor = Color.DarkCyan;
  940. chart1.Series[0].BorderColor = Color.DarkOliveGreen;
  941. chart1.Series[0].LabelBackColor = Color.Transparent;
  942. chart1.Series[0].LegendText = Yname;
  943. chart1.Legends[0].Title = _title;
  944. //chart1.ChartAreas[0].Area3DStyle.Enable3D = true;
  945. }
  946. /// <summary>
  947. /// 将新增打印进程信息写入静态文件
  948. /// </summary>
  949. /// <param name="lbl"></param>
  950. public static void WriteLbl(ApplicationClass lbl)
  951. {
  952. Process[] processes = System.Diagnostics.Process.GetProcessesByName("lppa");
  953. Dictionary<string, int> ProInf = new Dictionary<string, int>();
  954. int PID = 0;
  955. for (int i = 0; i < processes.Length; i++)
  956. {
  957. ProInf.Add(processes[i].StartTime.ToString(), processes[i].Id);
  958. }
  959. var temp = ProInf.Keys.Max();
  960. String str = SystemInf.ProcessesID + "|" + ProInf[temp];
  961. FileStream fs = new FileStream(SystemInf.CacheFolder + "lblprocess.txt", FileMode.Append, FileAccess.Write);
  962. StreamWriter sw = new StreamWriter(fs);
  963. sw.WriteLine(str, Encoding.UTF8);
  964. sw.Close();
  965. fs.Close();
  966. }
  967. public static Object GetCacheData(string ParamName)
  968. {
  969. try
  970. {
  971. Object o = null;
  972. //根据地址读取xml文件
  973. XmlDocument doc = new XmlDocument();
  974. XmlReaderSettings settings = new XmlReaderSettings();
  975. //忽略文档里面的注释
  976. settings.IgnoreComments = true;
  977. XmlReader reader = XmlReader.Create(SystemInf.CacheFilePath, settings);
  978. doc.Load(reader);
  979. //先得到根节点
  980. XmlNode rootNode = doc.SelectSingleNode("cacheInfo");
  981. //再由根节点去找制定的节点
  982. XmlNodeList nodeList = rootNode.ChildNodes;
  983. foreach (XmlNode node in nodeList)
  984. {
  985. //找到了这个节点名字
  986. if (node.Name == ParamName)
  987. {
  988. //返回节点的内容
  989. switch (((XmlElement)node).GetAttribute("Type"))
  990. {
  991. case "System.String":
  992. o = node.InnerText;
  993. break;
  994. case "System.Int32":
  995. o = int.Parse(node.InnerText);
  996. break;
  997. case "System.Boolean":
  998. o = node.InnerText == "True" ? true : false;
  999. break;
  1000. default:
  1001. break;
  1002. }
  1003. break;
  1004. }
  1005. }
  1006. //关闭reader
  1007. reader.Close();
  1008. if (o == null)
  1009. return "";
  1010. else
  1011. return o;
  1012. }
  1013. catch (Exception e)
  1014. {
  1015. LogManager.DoLog(e.Message);
  1016. return "";
  1017. }
  1018. }
  1019. public static void SetCacheData(string ParamName, object Value)
  1020. {
  1021. try
  1022. {
  1023. //根据地址读取xml文件
  1024. XmlDocument doc = new XmlDocument();
  1025. XmlReaderSettings settings = new XmlReaderSettings();
  1026. //忽略文档里面的注释
  1027. settings.IgnoreComments = true;
  1028. XmlReader reader = XmlReader.Create(SystemInf.CacheFilePath, settings);
  1029. doc.Load(reader);
  1030. //先得到根节点
  1031. XmlNode rootNode = doc.SelectSingleNode("cacheInfo");
  1032. //再由根节点去找制定的节点
  1033. XmlNodeList nodeList = rootNode.ChildNodes;
  1034. bool flag = false;
  1035. foreach (XmlNode node in nodeList)
  1036. {
  1037. //找到了这个节点名字
  1038. if (node.Name == ParamName)
  1039. {
  1040. //就直接赋值
  1041. node.InnerText = Value.ToString();
  1042. flag = true;
  1043. }
  1044. }
  1045. //如果没有该节点,就创建节点保存结果
  1046. if (!flag)
  1047. {
  1048. //创建节点
  1049. XmlElement newNode = doc.CreateElement(ParamName);
  1050. XmlAttribute attr = doc.CreateAttribute("Type");
  1051. attr.InnerText = Value.GetType().ToString();
  1052. newNode.InnerText = Value.ToString();
  1053. newNode.SetAttributeNode(attr);
  1054. //讲新建的节点挂到根节点上
  1055. rootNode.AppendChild(newNode);
  1056. }
  1057. //关闭Reader
  1058. reader.Close();
  1059. doc.Save(SystemInf.CacheFilePath);
  1060. }
  1061. catch (Exception e)
  1062. {
  1063. LogManager.DoLog(e.Message);
  1064. }
  1065. }
  1066. public static void ClosePrint(ApplicationClass lbl)
  1067. {
  1068. lblpro = lbl;
  1069. Thread close = new Thread(ClosePrintProcess);
  1070. close.Start();
  1071. }
  1072. static ApplicationClass lblpro;
  1073. private static void ClosePrintProcess()
  1074. {
  1075. try
  1076. {
  1077. lblpro.Quit();
  1078. }
  1079. catch (Exception)
  1080. {
  1081. }
  1082. }
  1083. public static bool connectState(string path)
  1084. {
  1085. return connectState(path, "a", "a");
  1086. }
  1087. /// <summary>
  1088. /// 连接远程共享文件夹
  1089. /// </summary>
  1090. /// <param name="path">远程共享文件夹的路径</param>
  1091. /// <param name="userName">用户名</param>
  1092. /// <param name="passWord">密码</param>
  1093. /// <returns></returns>
  1094. public static bool connectState(string path, string userName, string passWord)
  1095. {
  1096. bool Flag = false;
  1097. Process proc = new Process();
  1098. try
  1099. {
  1100. proc.StartInfo.FileName = "cmd.exe";
  1101. proc.StartInfo.UseShellExecute = false;
  1102. proc.StartInfo.RedirectStandardInput = true;
  1103. proc.StartInfo.RedirectStandardOutput = true;
  1104. proc.StartInfo.RedirectStandardError = true;
  1105. proc.StartInfo.CreateNoWindow = true;
  1106. proc.Start();
  1107. string dosLine = "net use " + path + " " + passWord + " /user:" + userName;
  1108. proc.StandardInput.WriteLine(dosLine);
  1109. proc.StandardInput.WriteLine("exit");
  1110. while (!proc.HasExited)
  1111. {
  1112. proc.WaitForExit(1);
  1113. }
  1114. string errormsg = proc.StandardError.ReadToEnd();
  1115. proc.StandardError.Close();
  1116. if (string.IsNullOrEmpty(errormsg))
  1117. {
  1118. Flag = true;
  1119. }
  1120. else
  1121. {
  1122. throw new Exception(errormsg);
  1123. }
  1124. }
  1125. catch (Exception ex)
  1126. {
  1127. LogManager.DoLog(ex.Message);
  1128. }
  1129. finally
  1130. {
  1131. proc.Close();
  1132. proc.Dispose();
  1133. }
  1134. return Flag;
  1135. }
  1136. /// <summary>
  1137. /// 向远程文件夹保存本地内容,或者从远程文件夹下载文件到本地
  1138. /// </summary>
  1139. /// <param name="src">要保存的文件的路径,如果保存文件到共享文件夹,这个路径就是本地文件路径如:@"D:\1.avi"</param>
  1140. /// <param name="dst">保存文件的路径,不含名称及扩展名</param>
  1141. /// <param name="fileName">保存文件的名称以及扩展名</param>
  1142. public static void Transport(string src, string dst, string fileName)
  1143. {
  1144. FileStream inFileStream = new FileStream(src, FileMode.Open);
  1145. if (!Directory.Exists(dst))
  1146. {
  1147. Directory.CreateDirectory(dst);
  1148. }
  1149. dst = dst + fileName;
  1150. FileStream outFileStream = new FileStream(dst, FileMode.OpenOrCreate);
  1151. byte[] buf = new byte[inFileStream.Length];
  1152. int byteCount;
  1153. while ((byteCount = inFileStream.Read(buf, 0, buf.Length)) > 0)
  1154. {
  1155. outFileStream.Write(buf, 0, byteCount);
  1156. }
  1157. inFileStream.Flush();
  1158. inFileStream.Close();
  1159. outFileStream.Flush();
  1160. outFileStream.Close();
  1161. }
  1162. /// <summary>
  1163. /// 取两个DataTable的交集,删除重复数据
  1164. /// </summary>
  1165. /// <param name="sourceDataTable"></param>
  1166. /// <param name="targetDataTable"></param>
  1167. /// <param name="primaryKey"></param>
  1168. /// <returns></returns>
  1169. public static DataTable DataTableMerge(DataTable sourceDataTable, DataTable targetDataTable, string primaryKey)
  1170. {
  1171. if (sourceDataTable != null || targetDataTable != null || !sourceDataTable.Equals(targetDataTable))
  1172. {
  1173. sourceDataTable.PrimaryKey = new DataColumn[] { sourceDataTable.Columns[primaryKey] };
  1174. DataTable dt = targetDataTable.Copy();
  1175. foreach (DataRow tRow in dt.Rows)
  1176. {
  1177. try
  1178. {
  1179. //拒绝自上次调用 System.Data.DataRow.AcceptChanges() 以来对该行进行的所有更改。
  1180. //因为行状态为DataRowState.Deleted时无法访问ItemArray的值
  1181. tRow.RejectChanges();
  1182. //在加载数据时关闭通知、索引维护和约束。
  1183. sourceDataTable.BeginLoadData();
  1184. //查找和更新特定行。如果找不到任何匹配行,则使用给定值创建新行。
  1185. DataRow temp = sourceDataTable.LoadDataRow(tRow.ItemArray, false);
  1186. sourceDataTable.EndLoadData();
  1187. sourceDataTable.Rows.Remove(temp);
  1188. }
  1189. catch
  1190. {
  1191. }
  1192. }
  1193. }
  1194. sourceDataTable.AcceptChanges();
  1195. return sourceDataTable;
  1196. }
  1197. /// <summary>
  1198. ///
  1199. /// </summary>
  1200. /// <param name="form"></param>
  1201. /// <param name="InOrOut">True表示打开窗体,False表示关闭窗体</param>
  1202. public static void FormStepInOrOut(Form form, bool InOrOut)
  1203. {
  1204. if (InOrOut)
  1205. {
  1206. for (int iNum = 0; iNum <= 10; iNum++)
  1207. {
  1208. //变更窗体的不透明度
  1209. form.Opacity = 0.1 * iNum;
  1210. //暂停
  1211. System.Threading.Thread.Sleep(20);
  1212. }
  1213. }
  1214. else
  1215. {
  1216. for (int iNum = 10; iNum >= 0; iNum--)
  1217. {
  1218. //变更窗体的不透明度
  1219. form.Opacity = 0.1 * iNum;
  1220. //暂停
  1221. System.Threading.Thread.Sleep(20);
  1222. }
  1223. }
  1224. }
  1225. }
  1226. }