BaseUtil.cs 33 KB

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