BaseUtil.cs 32 KB

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