GridHelper.cs 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.Data;
  5. using System.Diagnostics;
  6. using System.Drawing;
  7. using System.IO;
  8. using System.Linq;
  9. using System.Text;
  10. using System.Windows.Forms;
  11. using Core.Mes.Client.Comm.Tool;
  12. using Infragistics.Win.UltraWinGrid;
  13. using Infragistics.Win.UltraWinDataSource;
  14. using Infragistics.Win;
  15. namespace Core.Mes.Client.Comm.Control
  16. {
  17. /// <summary>
  18. /// Grid数据处理类
  19. /// </summary>
  20. public class GridHelper
  21. {
  22. /// <summary>
  23. /// 删除记录激活指定行
  24. /// </summary>
  25. /// <param name="grid">ultraGrid</param>
  26. /// <param name="delRowIndex">删除行的索引</param>
  27. public static void GridAfterDelRow_ReSelectRow(ref Infragistics.Win.UltraWinGrid.UltraGrid grid, int delRowIndex)
  28. {
  29. if (grid.Rows.Count == 0) return;
  30. if ((delRowIndex + 1) > grid.Rows.Count)
  31. {
  32. grid.Rows[delRowIndex - 1].Activate();
  33. grid.Rows[delRowIndex - 1].Selected = true;
  34. }
  35. else
  36. {
  37. grid.Rows[delRowIndex].Activate();
  38. grid.Rows[delRowIndex].Selected = true;
  39. }
  40. }
  41. /// <summary>
  42. /// 清除UltraGrid的DataSource的数据
  43. /// </summary>
  44. /// <param name="ulGrid"></param>
  45. public static void ClearGridDataSourceData(Infragistics.Win.UltraWinGrid.UltraGrid ulGrid)
  46. {
  47. if (ulGrid.DataSource is DataSet)
  48. {
  49. for (int i = ((DataSet)ulGrid.DataSource).Tables.Count - 1; i >= 0; i--)
  50. {
  51. ((DataSet)ulGrid.DataSource).Tables[i].Rows.Clear();
  52. }
  53. }
  54. }
  55. /// <summary>
  56. /// 将ultragrid的数据导出到Excel中
  57. /// </summary>
  58. /// <param name="ulGrid">要导出数据的ulGrid名称</param>
  59. /// <param name="sFileName">Excel文件名</param>
  60. public static void ulGridToExcel(Infragistics.Win.UltraWinGrid.UltraGrid ulGrid, string sFileName, bool showMergedCell)
  61. {
  62. try
  63. {
  64. Infragistics.Win.UltraWinGrid.ExcelExport.UltraGridExcelExporter ulGridExt = new Infragistics.Win.UltraWinGrid.ExcelExport.UltraGridExcelExporter();
  65. //ulGridExt.CellExporting += new Infragistics.Win.UltraWinGrid.ExcelExport.CellExportingEventHandler(ultraGridExcelExporter1_CellExporting);
  66. System.Windows.Forms.SaveFileDialog saveFileDialog1 = new SaveFileDialog();
  67. if (ulGrid.Rows.Count == 0)
  68. {
  69. MessageBox.Show("没有数据,无法导出!", "提示信息", MessageBoxButtons.OK, MessageBoxIcon.Warning);
  70. return;
  71. }
  72. if (showMergedCell)
  73. ulGridExt.CellExported += (sender, e) =>
  74. {
  75. // Get the cell being exporter
  76. var cell = e.GridRow.Cells[e.GridColumn];
  77. // Get the Merged cells.
  78. var mergedCells = cell.GetMergedCells();
  79. // Check to see if there are any merged cells. If not, we don't need to do anything.
  80. if (mergedCells != null)
  81. {
  82. // There are merged cells. Check to see if the cell being exported is the first one.
  83. var isFirstCell = true;
  84. foreach (var mergedCell in mergedCells)
  85. if (cell.Row.Index > mergedCell.Row.Index)
  86. {
  87. // If the cell being exported has a higher index that any of the cells
  88. // it is merged with, them it's not the first cell and we should already
  89. // have handled the merge.
  90. isFirstCell = false;
  91. break;
  92. }
  93. // If it is the first cell, merge this cell with the cells below it based on the
  94. // count of the merged cells.
  95. if (isFirstCell)
  96. {
  97. var rowIndex = e.CurrentRowIndex;
  98. var colIndex = e.CurrentColumnIndex;
  99. e.CurrentWorksheet.MergedCellsRegions.Add(
  100. rowIndex,
  101. colIndex,
  102. rowIndex + (mergedCells.Length - 1),
  103. colIndex);
  104. }
  105. }
  106. };
  107. saveFileDialog1.FileName = sFileName + DateTime.Now.ToString("yyyyMMdd");
  108. saveFileDialog1.Filter = "Excel文件(*.xls)|*.xls";
  109. if (saveFileDialog1.ShowDialog() == DialogResult.OK)
  110. {
  111. string sFullName = saveFileDialog1.FileName;
  112. ulGridExt.Export(ulGrid, sFullName);
  113. ProcessStartInfo p = new ProcessStartInfo(sFullName);
  114. p.WorkingDirectory = Path.GetDirectoryName(sFullName);
  115. Process.Start(p);
  116. }
  117. }
  118. catch (Exception ex)
  119. {
  120. MessageBox.Show("导出失败,原因:" + ex.Message, "错误信息", MessageBoxButtons.OK, MessageBoxIcon.Error);
  121. }
  122. }
  123. /// <summary>
  124. /// 将ultragrid的数据导出到Excel中
  125. /// </summary>
  126. /// <param name="ulGrid">要导出数据的ulGrid名称</param>
  127. /// <param name="sFileName">Excel文件名</param>
  128. public static void ulGridToExcel(Infragistics.Win.UltraWinGrid.UltraGrid ulGrid, string sFileName)
  129. {
  130. ulGridToExcel(ulGrid, sFileName, false);
  131. }
  132. /// <summary>
  133. /// 将ultragrid的数据导出到Excel中
  134. /// </summary>
  135. /// <param name="ulGridList">ulGrid组</param>
  136. /// <param name="sheetNameList">顺序对应的Sheet名称</param>
  137. /// <param name="sFileName">Excel文件名</param>
  138. public static void ulGridToExcel(ArrayList ulGridList, ArrayList sheetNameList, string sFileName)
  139. {
  140. try
  141. {
  142. Infragistics.Win.UltraWinGrid.ExcelExport.UltraGridExcelExporter ulGridExt = new Infragistics.Win.UltraWinGrid.ExcelExport.UltraGridExcelExporter();
  143. //ulGridExt.CellExporting += new Infragistics.Win.UltraWinGrid.ExcelExport.CellExportingEventHandler(ultraGridExcelExporter1_CellExporting);
  144. System.Windows.Forms.SaveFileDialog saveFileDialog1 = new SaveFileDialog();
  145. saveFileDialog1.FileName = sFileName + DateTime.Now.ToString("yyMMdd") + ".xls";
  146. if (saveFileDialog1.ShowDialog() == DialogResult.OK)
  147. {
  148. string sFullName = saveFileDialog1.FileName;
  149. Infragistics.Excel.Workbook workbook1 = new Infragistics.Excel.Workbook();
  150. for (int i = 0; i < ulGridList.Count; i++)
  151. {
  152. Infragistics.Win.UltraWinGrid.UltraGrid itemGrid = ulGridList[i] as Infragistics.Win.UltraWinGrid.UltraGrid;
  153. string sheetname = (sheetNameList.Count > i && sheetNameList[i].ToString() != "")
  154. ? sheetNameList[i].ToString() : ("sheet" + i.ToString());
  155. workbook1.Worksheets.Add(sheetname);
  156. //没有数据时会出错,所以没有数据的面不不调用
  157. if (itemGrid.Rows.Count > 0)
  158. ulGridExt.Export(itemGrid, workbook1.Worksheets[i]);
  159. }
  160. workbook1.Save(sFullName);
  161. }
  162. }
  163. catch (Exception ex)
  164. {
  165. MessageBox.Show("导出失败,原因:" + ex.Message);
  166. }
  167. }
  168. /// <summary>
  169. /// 清除Grid的列过滤
  170. /// </summary>
  171. /// <param name="ulGrid">Grid名称</param>
  172. public static void ClearUlGridFilter(Infragistics.Win.UltraWinGrid.UltraGrid ulGrid)
  173. {
  174. ulGrid.DisplayLayout.Bands[0].ColumnFilters.ClearAllFilters();
  175. }
  176. /// <summary>
  177. /// 增加Grid的列过滤
  178. /// </summary>
  179. /// <param name="ulGrid"></param>
  180. public static void AddUlGridFilter(Infragistics.Win.UltraWinGrid.UltraGrid ulGrid)
  181. {
  182. ulGrid.DisplayLayout.Override.AllowRowFiltering = Infragistics.Win.DefaultableBoolean.True;
  183. }
  184. /// <summary>
  185. /// 刷新Grid数据并根据数据调整Grid列宽
  186. /// </summary>
  187. /// <param name="ultraGrid">需要处理的Grid</param>
  188. public static void RefreshAndAutoSize(Infragistics.Win.UltraWinGrid.UltraGrid ultraGrid)
  189. {
  190. try
  191. {
  192. ultraGrid.DataBind();
  193. foreach (Infragistics.Win.UltraWinGrid.UltraGridBand band in ultraGrid.DisplayLayout.Bands)
  194. {
  195. foreach (Infragistics.Win.UltraWinGrid.UltraGridColumn column in band.Columns)
  196. {
  197. column.PerformAutoResize(Infragistics.Win.UltraWinGrid.PerformAutoSizeType.AllRowsInBand);
  198. }
  199. }
  200. ultraGrid.Refresh();
  201. }
  202. catch { }
  203. }
  204. /// <summary>
  205. /// 刷新Grid数据并根据数据调整Grid列宽 方法名错,弃用
  206. /// </summary>
  207. /// <param name="ultraGrid">需要处理的Grid</param>
  208. /// <param name="cols">不需要调整列宽的列</param>
  209. public static void RefreshAndAutoSizeExceptRows(Infragistics.Win.UltraWinGrid.UltraGrid ultraGrid, Infragistics.Win.UltraWinGrid.UltraGridColumn[] cols)
  210. {
  211. RefreshAndAutoSizeExceptColumns(ultraGrid, cols);
  212. }
  213. /// <summary>
  214. /// 刷新Grid数据并根据数据调整Grid列宽
  215. /// </summary>
  216. /// <param name="ultraGrid">需要处理的Grid</param>
  217. /// <param name="cols">不需要调整列宽的列</param>
  218. public static void RefreshAndAutoSizeExceptColumns(Infragistics.Win.UltraWinGrid.UltraGrid ultraGrid, Infragistics.Win.UltraWinGrid.UltraGridColumn[] cols)
  219. {
  220. try
  221. {
  222. ultraGrid.DataBind();
  223. foreach (Infragistics.Win.UltraWinGrid.UltraGridBand band in ultraGrid.DisplayLayout.Bands)
  224. {
  225. foreach (Infragistics.Win.UltraWinGrid.UltraGridColumn column in band.Columns)
  226. {
  227. if (cols != null && cols.Contains(column))
  228. {
  229. continue;
  230. }
  231. column.PerformAutoResize(Infragistics.Win.UltraWinGrid.PerformAutoSizeType.AllRowsInBand);
  232. }
  233. }
  234. ultraGrid.Refresh();
  235. }
  236. catch { }
  237. }
  238. /// <summary>
  239. /// 刷新Grid数据并根据数据调整Grid列宽
  240. /// </summary>
  241. /// <param name="ultraGrid">需要处理的Grid</param>
  242. /// <param name="cols">不需要调整列宽的列</param>
  243. public static void RefreshAndAutoSizeExceptColumns(Infragistics.Win.UltraWinGrid.UltraGrid ultraGrid, params string[] columns)
  244. {
  245. try
  246. {
  247. ultraGrid.DataBind();
  248. foreach (Infragistics.Win.UltraWinGrid.UltraGridBand band in ultraGrid.DisplayLayout.Bands)
  249. {
  250. foreach (Infragistics.Win.UltraWinGrid.UltraGridColumn column in band.Columns)
  251. {
  252. if (columns != null && columns.Contains(column.Key))
  253. {
  254. continue;
  255. }
  256. column.PerformAutoResize(Infragistics.Win.UltraWinGrid.PerformAutoSizeType.AllRowsInBand);
  257. }
  258. }
  259. ultraGrid.Refresh();
  260. }
  261. catch { }
  262. }
  263. /// <summary>
  264. /// 复制DataTable
  265. /// </summary>
  266. /// <param name="src">源DataTable</param>
  267. /// <param name="dest">目标DataTable</param>
  268. /// <param name="ClearExists">是否清除目标DataTable现有数据</param>
  269. public static void CopyDataToDatatable(ref DataTable src, ref DataTable dest, bool ClearExists)
  270. {
  271. if (src == null || dest == null)
  272. {
  273. return;
  274. }
  275. if (ClearExists)
  276. {
  277. dest.Rows.Clear();
  278. }
  279. DataRow CurRow, NewRow;
  280. for (int i = 0; i < src.Rows.Count; i++)
  281. {
  282. CurRow = src.Rows[i];
  283. NewRow = dest.NewRow();
  284. for (int j = 0; j < src.Columns.Count; j++)
  285. {
  286. try
  287. {
  288. if (dest.Columns.Contains(src.Columns[j].ColumnName))
  289. {
  290. NewRow[src.Columns[j].ColumnName] = CurRow[j];
  291. }
  292. }
  293. catch { }
  294. }
  295. dest.Rows.Add(NewRow);
  296. }
  297. }
  298. /// <summary>
  299. /// 复制DataTable
  300. /// </summary>
  301. /// <param name="src">源DataTable</param>
  302. /// <param name="dest">目标DataTable</param>
  303. /// <param name="ClearExists">是否清除目标DataTable现有数据</param>
  304. public static void CopyDataToDatatable(DataTable src, DataTable dest, bool ClearExists)
  305. {
  306. if (src == null || dest == null)
  307. {
  308. return;
  309. }
  310. if (ClearExists)
  311. {
  312. dest.Rows.Clear();
  313. }
  314. DataRow CurRow, NewRow;
  315. for (int i = 0; i < src.Rows.Count; i++)
  316. {
  317. CurRow = src.Rows[i];
  318. NewRow = dest.NewRow();
  319. for (int j = 0; j < src.Columns.Count; j++)
  320. {
  321. try
  322. {
  323. if (dest.Columns.Contains(src.Columns[j].ColumnName))
  324. {
  325. NewRow[src.Columns[j].ColumnName] = CurRow[j];
  326. }
  327. }
  328. catch { }
  329. }
  330. dest.Rows.Add(NewRow);
  331. }
  332. }
  333. /// <summary>
  334. /// 复制DataTable并添加ArrayList数据列
  335. /// </summary>
  336. /// <param name="src">源DataTable</param>
  337. /// <param name="dest">目标DataTable</param>
  338. /// <param name="ClearExists">是否清除目标DataTable现有数据</param>
  339. /// <param name="alistColumns">列名集合</param>
  340. /// <param name="alistValue">数据集合</param>
  341. public static void CopyDataToDatatable(ref DataTable src, ref DataTable dest, bool ClearExists, ArrayList alistColumns, ArrayList alistValue)
  342. {
  343. if (src == null || dest == null)
  344. {
  345. return;
  346. }
  347. if (ClearExists)
  348. {
  349. dest.Rows.Clear();
  350. }
  351. DataRow CurRow, NewRow;
  352. for (int i = 0; i < src.Rows.Count; i++)
  353. {
  354. CurRow = src.Rows[i];
  355. NewRow = dest.NewRow();
  356. for (int j = 0; j < src.Columns.Count; j++)
  357. {
  358. try
  359. {
  360. if (dest.Columns.Contains(src.Columns[j].ColumnName))
  361. {
  362. NewRow[src.Columns[j].ColumnName] = CurRow[j];
  363. }
  364. }
  365. catch { }
  366. }
  367. if (alistColumns != null && alistValue != null)
  368. {
  369. for (int idx = 0; idx < alistColumns.Count; idx++)
  370. {
  371. try
  372. {
  373. if (dest.Columns.Contains(alistColumns[idx].ToString()))
  374. {
  375. NewRow[alistColumns[idx].ToString()] = alistValue[idx];
  376. }
  377. }
  378. catch { }
  379. }
  380. }
  381. dest.Rows.Add(NewRow);
  382. }
  383. }
  384. /// <summary>
  385. /// Grid是否包含列
  386. /// </summary>
  387. /// <param name="grid">UltraGrid</param>
  388. /// <param name="strColumn">列名</param>
  389. /// <returns>有返回true 无返回false</returns>
  390. public static bool GridContainsColumn(ref Infragistics.Win.UltraWinGrid.UltraGrid grid, string strColumn)
  391. {
  392. for (int i = 0; i < grid.DisplayLayout.Bands[0].Columns.Count; i++)
  393. {
  394. if (grid.DisplayLayout.Bands[0].Columns[i].Key.Equals(strColumn))
  395. return true;
  396. }
  397. return false;
  398. }
  399. /// <summary>
  400. /// Grid是否包含列
  401. /// </summary>
  402. /// <param name="grid">UltraGrid</param>
  403. /// <param name="BandIndex">UltraGrid绑定数据集数组下标</param>
  404. /// <param name="strColumn">列名</param>
  405. /// <returns>有返回true 无返回false</returns>
  406. public static bool GridContainsColumn(ref Infragistics.Win.UltraWinGrid.UltraGrid grid, int BandIndex, string strColumn)
  407. {
  408. if (BandIndex <= grid.DisplayLayout.Bands.Count - 1)
  409. {
  410. for (int i = 0; i < grid.DisplayLayout.Bands[BandIndex].Columns.Count; i++)
  411. {
  412. if (grid.DisplayLayout.Bands[BandIndex].Columns[i].Key.Equals(strColumn))
  413. return true;
  414. }
  415. }
  416. return false;
  417. }
  418. /// <summary>
  419. /// 获取Table中最大编号,自增1返回。
  420. /// </summary>
  421. /// <param name="dt"></param>
  422. /// <returns></returns>
  423. public static string AutoCode(DataTable dt)
  424. {
  425. int maxCode = 0;
  426. //substring去除S001中的S,得到001。
  427. string maxCodeString = dt.Rows[0][0].ToString().Substring(1);
  428. //是否为数字
  429. if (StringUtil.IsNumber(maxCodeString))
  430. {
  431. maxCode = Convert.ToInt32(maxCodeString);
  432. }
  433. else
  434. {
  435. MessageBox.Show("传入编号格式有误,请查看代码!", "提示");
  436. }
  437. //比较得出最大id。
  438. for (int i = 1; i < dt.Rows.Count; i++)
  439. {
  440. string maxCodeString_Two = dt.Rows[i][0].ToString().Substring(1);
  441. int maxCode_Two = 0;
  442. if (StringUtil.IsNumber(maxCodeString_Two))
  443. {
  444. maxCode_Two = Convert.ToInt32(maxCodeString_Two);
  445. if (maxCode_Two > maxCode)
  446. {
  447. maxCode = maxCode_Two;
  448. }
  449. }
  450. }
  451. //新增行
  452. maxCode += 1;
  453. string codeTop = "";
  454. if (maxCode < 10)
  455. {
  456. codeTop = "00";
  457. }
  458. else if (maxCode < 100)
  459. {
  460. codeTop = "0";
  461. }
  462. //获取头文字D。
  463. string d = dt.Rows[0][0].ToString().Substring(0, 1);
  464. //头文字+0+最大数字
  465. string newCode = d + codeTop + maxCode;
  466. return newCode;
  467. }
  468. /// <summary>
  469. /// 根据关键字快速查找父表的UltaGridRow数组。
  470. /// </summary>
  471. /// <param name="grid">UltraGrid</param>
  472. /// <param name="columnNames">列名</param>
  473. /// <param name="keys">key值</param>
  474. /// <returns>UltraGridRow数组</returns>
  475. /// 2014年11月29日 15:25:26 cx
  476. public static UltraGridRow[] GetRowsWithKey(UltraGrid grid, string[] columnNames, string[] keys)
  477. {
  478. UltraGridRow[] rows = null;
  479. IQueryable<UltraGridRow> queryableRow = grid.Rows.AsQueryable();
  480. if (columnNames.Length != keys.Length)
  481. {
  482. throw new Exception("列名数组与key值数组长度不一致!");
  483. }
  484. for (int i = 0; i < columnNames.Length; i++)
  485. {
  486. queryableRow = GetQueryResult(queryableRow, columnNames[i], keys[i]);
  487. }
  488. rows = queryableRow.ToArray();
  489. return rows;
  490. }
  491. private static IQueryable<UltraGridRow> GetQueryResult(IQueryable<UltraGridRow> queryableRow,
  492. string column, string key)
  493. {
  494. queryableRow = from q in queryableRow
  495. where q.Cells[column].Value.ToString().ToUpper() == key.ToUpper()
  496. select q;
  497. return queryableRow;
  498. }
  499. /// <summary>
  500. /// 根据主键快速查找子表的UltaGridRow数组(结果为父行下的所有子行)。
  501. /// </summary>
  502. /// <param name="band">子表band</param>
  503. /// <param name="columnNames">列名</param>
  504. /// <param name="keys">key值</param>
  505. /// <returns>UltraGridRow数组</returns>
  506. /// 2014年11月29日 15:25:36 cx
  507. public static UltraGridRow[] GetRowsWithKey(UltraGridChildBand band , string[] columnNames, string[] keys)
  508. {
  509. UltraGridRow[] rows = null;
  510. IQueryable<UltraGridRow> queryableRow = band.Rows.AsQueryable();
  511. if (columnNames.Length != keys.Length)
  512. {
  513. throw new Exception("列名数组与key值数组长度不一致!");
  514. }
  515. for (int i = 0; i < columnNames.Length; i++)
  516. {
  517. queryableRow = GetQueryResult(queryableRow, columnNames[i], keys[i]);
  518. }
  519. rows = queryableRow.ToArray();
  520. return rows;
  521. }
  522. /// <summary>
  523. /// 设置指定的列只读
  524. /// </summary>
  525. /// <param name="band"></param>
  526. /// <param name="columnKeys"></param>
  527. public static void SetColumnsActive(UltraGridBand band, params string[] columnKeys)
  528. {
  529. foreach(string columnKey in columnKeys)
  530. {
  531. if(band.Columns.Exists(columnKey))
  532. {
  533. band.Columns[columnKey].CellActivation = Activation.ActivateOnly;
  534. }
  535. }
  536. }
  537. /// <summary>
  538. /// 设置不包含在指定列中的列只读。
  539. /// </summary>
  540. /// <param name="band"></param>
  541. /// <param name="columnKeys"></param>
  542. public static void SetExcludeColumnsActive(UltraGridBand band, params string[] columnKeys)
  543. {
  544. foreach (UltraGridColumn gridColumn in band.Columns)
  545. {
  546. if (columnKeys.Contains(gridColumn.Key) == false)
  547. {
  548. gridColumn.CellActivation = Activation.ActivateOnly;
  549. }
  550. }
  551. }
  552. /// <summary>
  553. /// 设置不包含在指定列中的列只读,并且设置可编辑列颜色
  554. /// </summary>
  555. /// <param name="band"></param>
  556. /// <param name="columnKeys"></param>
  557. public static void SetColumnsActivateAndColor(UltraGridBand band, params string[] columnKeys)
  558. {
  559. foreach (UltraGridColumn gridColumn in band.Columns)
  560. {
  561. if (columnKeys.Contains(gridColumn.Key) == false)
  562. gridColumn.CellActivation = Activation.ActivateOnly;
  563. else if (gridColumn.Key != "Choose")
  564. gridColumn.CellAppearance.BackColor = Color.FromArgb(((int)(((byte)(255)))), ((int)(((byte)(255)))), ((int)(((byte)(128)))));
  565. else
  566. gridColumn.CellActivation = Activation.AllowEdit;
  567. }
  568. }
  569. /// <summary>
  570. /// 初始化UltraGrid Card模式数据
  571. /// </summary>
  572. /// <param name="ultDataSource"></param>
  573. /// <param name="ultraGrid"></param>
  574. public static void InitCardGrid(UltraDataSource ultDataSource, UltraGrid ultraGrid)
  575. {
  576. try
  577. {
  578. int iColumnsCount = ultDataSource.Band.Columns.Count;
  579. object[] obj = new object[iColumnsCount];
  580. ultDataSource.Rows.Clear();
  581. for (int i = 0; i < ultDataSource.Band.Columns.Count; i++)
  582. {
  583. try
  584. {
  585. if (ultDataSource.Band.Columns[i].DataType == typeof(Bitmap) ||
  586. ultDataSource.Band.Columns[i].DataType == typeof(Image))
  587. obj[i] = null;
  588. else if (ultDataSource.Band.Columns[i].DataType == typeof(DateTime))
  589. obj[i] = DBNull.Value;
  590. else if (ultDataSource.Band.Columns[i].DataType == typeof(decimal) ||
  591. ultDataSource.Band.Columns[i].DataType == typeof(double) ||
  592. ultDataSource.Band.Columns[i].DataType == typeof(Single))
  593. obj[i] = 0;
  594. else
  595. obj[i] = "";
  596. }
  597. catch { }
  598. try
  599. {
  600. ultraGrid.DisplayLayout.Bands[0].Columns[i].Header.Appearance.FontData.Bold = DefaultableBoolean.False;
  601. }
  602. catch { }
  603. }
  604. ultDataSource.Rows.Add(obj);
  605. ultraGrid.UpdateData();
  606. }
  607. catch { }
  608. }
  609. /// <summary>
  610. /// 隐藏指定的列
  611. /// </summary>
  612. /// <param name="gridBand">UltraGridBand</param>
  613. /// <param name="columnKeys">需要隐藏的列</param>
  614. public static void HidenColumns(UltraGridBand gridBand, params string[] columnKeys)
  615. {
  616. foreach (string columnKey in columnKeys)
  617. {
  618. if (gridBand.Columns.Exists(columnKey))
  619. {
  620. gridBand.Columns[columnKey].Hidden = true;
  621. }
  622. }
  623. }
  624. /// <summary>
  625. /// 隐藏不包含在指定列中的列。
  626. /// </summary>
  627. /// <param name="gridBand">UltraGridBand</param>
  628. /// <param name="columnKeys">不需要隐藏的列</param>
  629. public static void HidenExcludeColumns(UltraGridBand gridBand, params string[] columnKeys)
  630. {
  631. foreach(UltraGridColumn gridColumn in gridBand.Columns)
  632. {
  633. if (columnKeys.Contains(gridColumn.Key) == false)
  634. {
  635. gridColumn.Hidden = true;
  636. }
  637. }
  638. }
  639. /// <summary>
  640. /// UltraGridBand列设置全部只读。
  641. /// </summary>
  642. /// <param name="grid">UltraGridBand</param>
  643. public static void SetAllColumnsActive(UltraGrid grid)
  644. {
  645. foreach (UltraGridColumn column in grid.DisplayLayout.Bands[0].Columns)
  646. {
  647. column.CellActivation = Activation.ActivateOnly;
  648. }
  649. }
  650. }
  651. }