BaseUtil.cs 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828
  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)
  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. /// <summary>
  511. /// 需要SQL的顺序和DGV的列的顺序一致
  512. /// </summary>
  513. /// <param name="dgv"></param>
  514. /// <param name="dt"></param>
  515. /// <param name="CheckBox"></param>
  516. public static void FillExpandDgvWithDataTable(DataGridViewExpand dgv, DataTable dt, bool CheckBox)
  517. {
  518. CleanDGVData(dgv);
  519. if (CheckBox)
  520. {
  521. for (int i = 0; i < dt.Rows.Count; i++)
  522. {
  523. CollapseDataGridViewRow collapseRow = new CollapseDataGridViewRow();
  524. collapseRow.IsCollapse = true;
  525. DataGridViewCheckBoxCell checkcell = new DataGridViewCheckBoxCell();
  526. collapseRow.Cells.Add(checkcell);
  527. //因为DGV中可能有空置的列多出,所以需要用DataTable的列进行循环
  528. for (int j = 1; j < dt.Columns.Count; j++)
  529. {
  530. DataGridViewTextBoxCell textcell = new DataGridViewTextBoxCell();
  531. textcell.Value = dt.Rows[i][j - 1].ToString();
  532. collapseRow.Cells.Add(textcell);
  533. textcell.ReadOnly = true;
  534. }
  535. collapseRow.Tag = "MainRow";
  536. dgv.Rows.Add(collapseRow);
  537. }
  538. }
  539. else
  540. {
  541. for (int i = 0; i < dt.Rows.Count; i++)
  542. {
  543. CollapseDataGridViewRow collapseRow = new CollapseDataGridViewRow();
  544. collapseRow.IsCollapse = true;
  545. for (int j = 1; j < dt.Columns.Count; j++)
  546. {
  547. DataGridViewTextBoxCell textcell = new DataGridViewTextBoxCell();
  548. textcell.Value = dt.Rows[i][j - 1].ToString();
  549. collapseRow.Cells.Add(textcell);
  550. }
  551. dgv.Rows.Add(collapseRow);
  552. collapseRow.ReadOnly = true;
  553. }
  554. }
  555. }
  556. /// <summary>
  557. /// 用于给DGV中的Combox列赋静态值
  558. /// </summary>
  559. /// <param name="dgvc"></param>
  560. /// <param name="displayField"></param>
  561. /// <param name="valueField"></param>
  562. /// <param name="Value"></param>
  563. /// <returns></returns>
  564. public static void SetDgvColumnComboxData(DataGridViewComboBoxColumn dgvc, string DataPropertyName, string displayField, string valueField, string[] Value)
  565. {
  566. DataTable dt = new DataTable();
  567. dt.Columns.Add(displayField);
  568. dt.Columns.Add(valueField);
  569. for (int i = 0; i < Value.Length; i++)
  570. {
  571. DataGridViewRow row = new DataGridViewRow();
  572. dt.Rows.Add(row);
  573. dt.Rows[i][displayField] = Value[i].Split('#')[0];
  574. dt.Rows[i][valueField] = Value[i].Split('#')[1];
  575. }
  576. dgvc.DataPropertyName = DataPropertyName;
  577. dgvc.DataSource = dt;
  578. dgvc.DisplayMember = displayField;
  579. dgvc.ValueMember = valueField;
  580. }
  581. /// <summary>
  582. /// 用于给DGV中的ComboxCell赋静态值
  583. /// </summary>
  584. /// <param name="dgvcc"></param>
  585. /// <param name="displayField"></param>
  586. /// <param name="valueField"></param>
  587. /// <param name="Value"></param>
  588. public static void SetDGVCellComboxData(DataGridViewComboBoxCell dgvcc, string displayField, string valueField, string[] Value)
  589. {
  590. DataTable dt = new DataTable();
  591. dt.Columns.Add(displayField);
  592. dt.Columns.Add(valueField);
  593. for (int i = 0; i < Value.Length; i++)
  594. {
  595. DataRow dr = dt.NewRow();
  596. dr[displayField] = Value[i].Split('#')[0];
  597. dr[valueField] = Value[i].Split('#')[1];
  598. dt.Rows.Add(dr);
  599. }
  600. dgvcc.DisplayMember = displayField;
  601. dgvcc.ValueMember = valueField;
  602. dgvcc.DataSource = dt;
  603. }
  604. /// <summary>
  605. /// 获取刷选的SQL语句,传入的是TextBox的Control,传入的SQL不带Where条件
  606. /// </summary>
  607. /// <param name="SQL"></param>
  608. /// <param name="Condition"></param>
  609. /// <returns></returns>
  610. public static string GetScreenSqlCondition(params Control[] Condition)
  611. {
  612. string condition = "";
  613. //用于统计传入的控件的空值数
  614. int EmptyControlCount = 0;
  615. for (int i = 0; i < Condition.Length; i++)
  616. {
  617. //如果Text不为空再进行条件的拼接
  618. if (Condition[i].Text != "")
  619. {
  620. if (Condition[i] is ComboBox)
  621. {
  622. condition += "(" + Condition[i].Tag + " like " + "'%" + (Condition[i] as ComboBox).SelectedValue + "%' )";
  623. }
  624. else
  625. {
  626. condition += "(" + Condition[i].Tag + " like " + "'%" + Condition[i].Text + "%' )";
  627. }
  628. //如果不是最后要判断之后有没有空值的如果有一个Text的值不为空都需要添加and
  629. //添加了一次And之后跳出循环,因为如果后面多项有值会重复添加and
  630. for (int j = i + 1; j < Condition.Length; j++)
  631. {
  632. if (j < Condition.Length)
  633. {
  634. if (Condition[j].Text != "")
  635. {
  636. condition += " and ";
  637. break;
  638. }
  639. }
  640. }
  641. }
  642. else
  643. {
  644. EmptyControlCount = EmptyControlCount + 1;
  645. }
  646. }
  647. //如果所有的控件传入的都是空值则返回也为空
  648. if (EmptyControlCount == Condition.Length)
  649. return "";
  650. else
  651. condition = " where " + condition;
  652. return condition;
  653. }
  654. public static void CleanDataGridView(DataGridView dgv)
  655. {
  656. for (int i = dgv.Columns.Count - 1; i >= 0; i--)
  657. {
  658. dgv.Columns.RemoveAt(i);
  659. }
  660. }
  661. /// <summary>
  662. /// 取出SQL中的参数占位符
  663. /// </summary>
  664. /// <param name="SQL"></param>
  665. /// <returns></returns>
  666. public static string[] GetParamFromSQL(string SQL)
  667. {
  668. string[] par = SQL.Split(':');
  669. //用来存参数的数组
  670. StringBuilder[] addpar = new StringBuilder[par.Length - 1];
  671. string[] param = new string[par.Length - 1];
  672. for (int i = 0; i < par.Length - 1; i++)
  673. {
  674. //新建一个char类型的数组用来存储每个字节的变量
  675. char[] c = par[i + 1].ToCharArray();
  676. addpar[i] = new StringBuilder();
  677. for (int j = 0; j < c.Length; j++)
  678. {
  679. if (c[j] != ' ' && c[j] != ',' && c[j] != ')')
  680. {
  681. addpar[i].Append(c[j]);
  682. }
  683. else
  684. {
  685. break;
  686. }
  687. }
  688. }
  689. for (int i = 0; i < par.Length - 1; i++)
  690. {
  691. param[i] = addpar[i].ToString();
  692. }
  693. return param;
  694. }
  695. public static void SetFormCenter(Form form)
  696. {
  697. form.StartPosition = FormStartPosition.CenterParent;
  698. }
  699. /// <summary>
  700. /// 设置DataGridView的指定列可编辑
  701. /// </summary>
  702. /// <param name="DGV"></param>
  703. /// <param name="EditAbleField"></param>
  704. public static void SetDataGridViewReadOnly(DataGridView DGV, string[] EditAbleField)
  705. {
  706. foreach (DataGridViewColumn dc in DGV.Columns)
  707. {
  708. dc.ReadOnly = true;
  709. foreach (string s in EditAbleField)
  710. {
  711. if (dc.Name.ToLower() == s.ToLower())
  712. {
  713. DGV.Columns[dc.Name].ReadOnly = false;
  714. }
  715. }
  716. }
  717. }
  718. //判断带有CheckBox的DGV是否有项目勾选了
  719. public static DataTable DGVIfChecked(DataGridView dgv)
  720. {
  721. int CheckCount = 0;
  722. DataTable dt = new DataTable();
  723. //第一列是勾选框,排除在循环之外
  724. for (int i = 1; i < dgv.Columns.Count; i++)
  725. {
  726. dt.Columns.Add(dgv.Columns[i].Name);
  727. }
  728. for (int i = 0; i < dgv.RowCount; i++)
  729. {
  730. if (dgv.Rows[i].Cells[0].Value != null)
  731. {
  732. if (dgv.Rows[i].Cells[0].Value.ToString() == "True")
  733. {
  734. if (dgv.Rows[i].Tag.ToString() == "SonRow")
  735. {
  736. DataRow dr = dt.NewRow();
  737. for (int j = 1; j < dgv.ColumnCount; j++)
  738. {
  739. dr[dgv.Columns[j].Name] = dgv.Rows[i].Cells[j].FormattedValue;
  740. }
  741. dt.Rows.Add(dr);
  742. CheckCount++;
  743. }
  744. }
  745. }
  746. }
  747. //判断是否勾选了明细
  748. if (CheckCount == 0)
  749. return null;
  750. return dt;
  751. }
  752. /// <summary>
  753. /// 设置只允许输入数字
  754. /// </summary>
  755. /// <param name="sender"></param>
  756. /// <param name="e"></param>
  757. public static void NumOnly(object sender, KeyPressEventArgs e)
  758. {
  759. if (e.KeyChar != '\b')//这是允许输入退格键
  760. {
  761. if ((e.KeyChar < '0') || (e.KeyChar > '9'))//这是允许输入0-9数字
  762. {
  763. e.Handled = true;
  764. }
  765. }
  766. }
  767. public static string AddField(string[] Fields)
  768. {
  769. string sql = " ";
  770. foreach (string field in Fields)
  771. sql += field + ",";
  772. return sql.Substring(0, sql.Length - 1);
  773. }
  774. /// <summary>
  775. /// 筛选DataTable
  776. /// </summary>
  777. /// <param name="dt"></param>
  778. /// <param name="condition"></param>
  779. /// <returns></returns>
  780. public static DataTable filterDataTable(DataTable dt, String condition)
  781. {
  782. //获取筛选条件中的列名,值
  783. DataRow[] dataRows = dt.Select(condition);
  784. Console.WriteLine(dataRows.Length + "");
  785. DataTable ndt = dt.Clone();
  786. for (int i = 0; i < dataRows.Length; i++)
  787. {
  788. ndt.Rows.Add(dataRows[i].ItemArray);
  789. }
  790. return ndt;
  791. }
  792. }
  793. }