BaseUtil.cs 31 KB

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