GridHelper.cs 28 KB

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