BaseUtil.cs 34 KB

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