BaseUtil.cs 32 KB

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