BaseUtil.cs 36 KB

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