BaseUtil.cs 31 KB

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