ClsControlPack.cs 75 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702
  1. using Infragistics.Excel;
  2. using Infragistics.Win;
  3. using Infragistics.Win.Layout;
  4. using Infragistics.Win.Misc;
  5. using Infragistics.Win.UltraWinDataSource;
  6. using Infragistics.Win.UltraWinEditors;
  7. using Infragistics.Win.UltraWinGrid;
  8. using Infragistics.Win.UltraWinGrid.ExcelExport;
  9. using Infragistics.Win.UltraWinTabControl;
  10. using System;
  11. using System.Collections;
  12. using System.Collections.Generic;
  13. using System.ComponentModel;
  14. using System.Data;
  15. using System.Diagnostics;
  16. using System.Drawing;
  17. using System.Drawing.Printing;
  18. using System.IO;
  19. using System.Net.NetworkInformation;
  20. using System.Reflection;
  21. using System.Text;
  22. using System.Text.RegularExpressions;
  23. using System.Windows.Forms;
  24. namespace CarLocalMeter
  25. {
  26. public class ClsControlPack
  27. {
  28. private Log mylog = Log.GetInstance();
  29. /// <summary>
  30. /// 获取子窗体弹出的位置坐标
  31. /// </summary>
  32. /// <param name="ChildWindowSize"></param>
  33. /// <returns></returns>
  34. public static Point GetChildWindowLocation(Size ChildWindowSize)
  35. {
  36. Point point = Cursor.Position;
  37. Size size = Cursor.Current.Size;
  38. int width = point.X + ChildWindowSize.Width - Screen.PrimaryScreen.Bounds.Width;
  39. int height = point.Y + ChildWindowSize.Height - Screen.PrimaryScreen.Bounds.Height;
  40. width = (width > 0) ? (point.X - width - size.Width) : point.X; // X坐标
  41. height = (height > 0) ? (point.Y - height - size.Height) : point.Y; // Y坐标
  42. return new Point(width, height);
  43. }
  44. /// <summary>
  45. /// 按字段,过滤UltraGrid信息
  46. /// </summary>
  47. /// <param name="ug"></param>
  48. /// <param name="columnName"></param>
  49. /// <param name="filter"></param>
  50. public static void SetUltraGridFilter(UltraGrid ug, string columnName, string filter)
  51. {
  52. if (ug != null && ug.Rows.Count > 0)
  53. {
  54. ug.DisplayLayout.Bands[0].ColumnFilters[columnName].FilterConditions.Clear();
  55. if (!string.IsNullOrEmpty(filter))
  56. {
  57. ug.DisplayLayout.Bands[0].ColumnFilters[columnName].FilterConditions.Add(
  58. Infragistics.Win.UltraWinGrid.FilterComparisionOperator.Contains, filter);
  59. }
  60. }
  61. }
  62. public static void ClearUltraGridFilter(UltraGrid ug)
  63. {
  64. if (ug != null)
  65. {
  66. ug.DisplayLayout.Bands[0].ColumnFilters.ClearAllFilters();
  67. }
  68. }
  69. /// <summary>
  70. /// DataTable数据拷贝
  71. /// </summary>
  72. /// <param name="src">源</param>
  73. /// <param name="dest">目标</param>
  74. /// <param name="ClearExists">是否清除目标DataTable现有数据</param>
  75. public static void CopyDataToDatatable(ref DataTable src, ref DataTable dest, bool ClearExists)
  76. {
  77. try
  78. {
  79. if (src == null)
  80. {
  81. dest = null;
  82. return;
  83. }
  84. if (dest == null)
  85. {
  86. dest = new DataTable();
  87. }
  88. if (ClearExists)
  89. {
  90. if (dest != null) dest.Rows.Clear();
  91. }
  92. DataRow CurRow, NewRow;
  93. if (src != null && src.Rows != null && src.Rows.Count > 0)
  94. for (int i = 0; i < src.Rows.Count; i++)
  95. {
  96. CurRow = src.Rows[i];
  97. NewRow = dest.NewRow();
  98. for (int j = 0; j < src.Columns.Count; j++)
  99. {
  100. try
  101. {
  102. if (dest.Columns.Contains(src.Columns[j].ColumnName))
  103. {
  104. NewRow[src.Columns[j].ColumnName] = CurRow[j];
  105. }
  106. }
  107. catch { }
  108. }
  109. dest.Rows.Add(NewRow);
  110. }
  111. dest.AcceptChanges();
  112. }
  113. catch (Exception ex)
  114. {
  115. MessageBox.Show("公共方法CopyDataToDatatable报错!错误内容:\r\n" + ex.Message);
  116. }
  117. }
  118. /// <summary>
  119. /// UltraGrid列宽自适应
  120. /// </summary>
  121. /// <param name="ultraGrid"></param>
  122. public static void RefreshAndAutoSize(UltraGrid ultraGrid, bool childFlag = false)
  123. {
  124. int count = 0;
  125. try
  126. {
  127. count = 1;
  128. /**
  129. ultraGrid.BeginUpdate();
  130. if (ultraGrid != null && ultraGrid.Rows.Count > 0)
  131. {
  132. foreach (UltraGridBand band in ultraGrid.DisplayLayout.Bands)
  133. {
  134. foreach (UltraGridColumn column in band.Columns)
  135. {
  136. column.PerformAutoResize(PerformAutoSizeType.AllRowsInBand, band.ColHeadersVisible ? true : false);
  137. }
  138. }
  139. }
  140. ultraGrid.EndUpdate();
  141. */
  142. if (ultraGrid == null || ultraGrid.Rows == null || ultraGrid.Rows.Count == 0)
  143. {
  144. ultraGrid.Update();
  145. ultraGrid.Refresh();
  146. return;
  147. }
  148. ultraGrid.DataBind(); //此行代码会报错,暂未解决;
  149. count = 2;
  150. ultraGrid.Update();
  151. count = 3;
  152. bool childbandsAutoSize = childFlag; //引用的浩鑫的代码;默认为false
  153. if (childbandsAutoSize)
  154. {
  155. foreach (UltraGridBand band in ultraGrid.DisplayLayout.Bands)
  156. {
  157. count = 4;
  158. if (band.Index > 0)
  159. {
  160. count = 5;
  161. band.RowLayoutStyle = RowLayoutStyle.GroupLayout;
  162. }
  163. count = 6;
  164. band.PerformAutoResizeColumns(true, PerformAutoSizeType.AllRowsInBand);
  165. }
  166. }
  167. else
  168. {
  169. foreach (UltraGridBand band in ultraGrid.DisplayLayout.Bands)
  170. {
  171. count = 7;
  172. band.PerformAutoResizeColumns(true, PerformAutoSizeType.VisibleRows);
  173. }
  174. }
  175. count = 8;
  176. ultraGrid.Refresh();
  177. count = 9;
  178. }
  179. catch (Exception ex)
  180. {
  181. // MessageBox.Show(ultraGrid.Name + "公共方法RefreshAndAutoSize报错,位置【" + count + "】!错误内容:\r\n" + ex.Message);
  182. }
  183. }
  184. /// <summary>
  185. /// 设置UltraComboEditor项目集合
  186. /// </summary>
  187. /// <param name="cbEdt"></param>
  188. /// <param name="vlist"></param>
  189. /// <param name="bNull">是否加入空项</param>
  190. /// <returns></returns>
  191. public static bool SetUltraComboEditor(ref UltraComboEditor cbEdt, ref ValueList vlist, bool bNull)
  192. {
  193. if (cbEdt == null || vlist == null)
  194. return false;
  195. cbEdt.Items.Clear();
  196. if (bNull)
  197. cbEdt.Items.Add(null, "");
  198. for (int i = 0; i < vlist.ValueListItems.Count; i++)
  199. {
  200. try
  201. {
  202. cbEdt.Items.Add(vlist.ValueListItems[i].DataValue, vlist.ValueListItems[i].DisplayText);
  203. }
  204. catch { }
  205. }
  206. return true;
  207. }
  208. /// <summary>
  209. /// 载入下拉框中的数据
  210. /// </summary>
  211. /// <param name="cmb"></param>
  212. /// <param name="table"></param>
  213. /// <param name="ColumnValue"></param>
  214. /// <param name="ColumnDisplay"></param>
  215. public static void SetUltraComboDataSource(UltraCombo cmb, DataTable table, string ColumnValue, string ColumnDisplay)
  216. {
  217. if (table == null || !table.Columns.Contains(ColumnValue) || !table.Columns.Contains(ColumnDisplay)) return;
  218. try
  219. {
  220. if (cmb != null)
  221. {
  222. cmb.DataSource = table;
  223. cmb.DisplayLayout.Override.TipStyleScroll = TipStyle.Hide;
  224. cmb.DisplayLayout.Override.BorderStyleCell = UIElementBorderStyle.None;
  225. cmb.DisplayLayout.Override.BorderStyleRow = UIElementBorderStyle.None;
  226. cmb.DisplayLayout.Override.SelectTypeRow = SelectType.Single;
  227. cmb.DisplayLayout.Override.HeaderStyle = HeaderStyle.Standard;
  228. cmb.DisplayLayout.Override.HeaderAppearance.TextHAlign = HAlign.Center;
  229. cmb.DisplayLayout.Override.RowAppearance.TextVAlign = VAlign.Middle;
  230. cmb.DisplayLayout.Override.ActiveRowAppearance.BackColor = Color.FromArgb(51, 153, 255);
  231. cmb.DisplayLayout.Override.ActiveRowAppearance.ForeColor = Color.FromArgb(255, 255, 255);
  232. cmb.DisplayLayout.Override.SelectedRowAppearance.BackColor = Color.FromArgb(51, 153, 255);
  233. cmb.DisplayLayout.Override.SelectedRowAppearance.ForeColor = Color.FromArgb(255, 255, 255);
  234. //cmb.DisplayLayout.Override.ActiveRowAppearance.FontData.Bold = DefaultableBoolean.True;
  235. foreach (UltraGridBand band in cmb.DisplayLayout.Bands)
  236. {
  237. band.ColHeadersVisible = false;
  238. foreach (UltraGridColumn column in band.Columns)
  239. {
  240. if (!column.Key.Equals(ColumnDisplay)) column.Hidden = true;
  241. column.Width = cmb.Width;
  242. //column.PerformAutoResize(PerformAutoSizeType.AllRowsInBand);
  243. }
  244. }
  245. cmb.ValueMember = ColumnValue;
  246. cmb.DisplayMember = ColumnDisplay;
  247. cmb.DropDownWidth = -1;
  248. }
  249. }
  250. catch { }
  251. }
  252. public static ArrayList GetSelectedRows(UltraGrid ug, string ColumnName, bool IncludeFilterOut = true)
  253. {
  254. int BandIndex = 0;
  255. ArrayList selectedRows = new ArrayList();
  256. try
  257. {
  258. if (!ug.DisplayLayout.Bands[BandIndex].Columns.Exists(ColumnName)) return selectedRows;
  259. if (!ug.DisplayLayout.Bands[BandIndex].Columns.Exists("UNCK")) return selectedRows;
  260. for (int idx = 0; idx < ug.Rows.Count; idx++)
  261. {
  262. UltraGridRow ur = ug.Rows[idx];
  263. if (ur.IsFilteredOut && !IncludeFilterOut) continue;
  264. object v = ur.GetCellValue(ug.DisplayLayout.Bands[BandIndex].Columns["UNCK"]);
  265. if (v.GetType() == typeof(Boolean) || v.GetType() == typeof(bool))
  266. {
  267. if ((bool)v) selectedRows.Add(ur.GetCellText(ug.DisplayLayout.Bands[BandIndex].Columns[ColumnName]));
  268. }
  269. else if (v.GetType() == typeof(string) || v.GetType() == typeof(String))
  270. {
  271. if (!(v.ToString().ToUpper() == "FALSE" || v.ToString().ToUpper() == "0"))
  272. selectedRows.Add(ur.GetCellText(ug.DisplayLayout.Bands[BandIndex].Columns[ColumnName]));
  273. }
  274. else
  275. {
  276. if (((int)v) != 0)
  277. selectedRows.Add(ur.GetCellText(ug.DisplayLayout.Bands[BandIndex].Columns[ColumnName]));
  278. }
  279. }
  280. return selectedRows;
  281. }
  282. catch (Exception ex)
  283. {
  284. MessageBox.Show("Clean方法异常,请关闭界面并稍后打开,或联系管理员" + ex);
  285. return selectedRows;
  286. }
  287. }
  288. /// <summary>
  289. /// 设置ultraGrid的激活单元格
  290. /// </summary>
  291. /// <param name="ultraGrid"></param>
  292. /// <param name="RowIndexNo"></param>
  293. /// <param name="ColumnName"></param>
  294. /// <param name="EnterEditMode"></param>
  295. public static void SetUltraGridActiveCell(ref UltraGrid ultraGrid, int RowIndexNo, string ColumnName, bool EnterEditMode)
  296. {
  297. try
  298. {
  299. if (ultraGrid != null && RowIndexNo >= 0 && ultraGrid.Rows.Count >= RowIndexNo + 1)
  300. {
  301. ultraGrid.ActiveCell = ultraGrid.Rows[RowIndexNo].Cells[ColumnName];
  302. if (EnterEditMode)
  303. ultraGrid.PerformAction(UltraGridAction.EnterEditMode);
  304. }
  305. }
  306. catch { }
  307. }
  308. #region ultraGrid设置
  309. public static void ClearEvent(Control control, string eventname)
  310. {
  311. if (control == null) return;
  312. if (string.IsNullOrEmpty(eventname)) return;
  313. BindingFlags mPropertyFlags = BindingFlags.Instance | BindingFlags.Public | BindingFlags.Static | BindingFlags.NonPublic;
  314. BindingFlags mFieldFlags = BindingFlags.Static | BindingFlags.NonPublic;
  315. Type controlType = typeof(Control);
  316. PropertyInfo propertyInfo = controlType.GetProperty("Events", mPropertyFlags);
  317. EventHandlerList eventHandlerList = (EventHandlerList)propertyInfo.GetValue(control, null);
  318. FieldInfo fieldInfo = (typeof(System.Windows.Forms.Control)).GetField("Event" + eventname, mFieldFlags);
  319. Delegate d = eventHandlerList[fieldInfo.GetValue(control)];
  320. if (d == null) return;
  321. EventInfo eventInfo = controlType.GetEvent(eventname);
  322. foreach (Delegate dx in d.GetInvocationList())
  323. eventInfo.RemoveEventHandler(control, dx);
  324. }
  325. public static void SetUltraGridStyle(Control Parent, bool AutoSizeUltraGrid)
  326. {
  327. UltraGrid grid = null;
  328. foreach (Control ctrl in Parent.Controls)
  329. {
  330. try
  331. {
  332. if (ctrl.GetType().Equals(typeof(UltraGrid)))
  333. {
  334. grid = (UltraGrid)ctrl;
  335. if (grid == null) continue;
  336. try
  337. {
  338. if (grid.DisplayLayout.Bands.Count > 0)
  339. {
  340. if (grid.DisplayLayout.Bands[0].CardView)
  341. {
  342. grid.DisplayLayout.Appearance.ResetBorderColor();
  343. grid.DisplayLayout.BorderStyle = UIElementBorderStyle.None;
  344. grid.DisplayLayout.Override.CardAreaAppearance.BackColor = _BACKCOLOR;
  345. grid.DisplayLayout.Override.CardAreaAppearance.BackColor2 = _BACKCOLOR;
  346. grid.DisplayLayout.Override.HeaderAppearance.BackColor = _BACKCOLOR;
  347. grid.DisplayLayout.Override.HeaderAppearance.BackColor2 = _BACKCOLOR;
  348. grid.DisplayLayout.Override.BorderStyleRow = UIElementBorderStyle.None;
  349. }
  350. else
  351. {
  352. grid.DisplayLayout.UseFixedHeaders = true;
  353. grid.DisplayLayout.BorderStyle = UIElementBorderStyle.Default;
  354. grid.DisplayLayout.Appearance.BorderColor = Color.DarkGray;
  355. grid.DisplayLayout.ViewStyleBand = ViewStyleBand.Vertical;
  356. grid.DisplayLayout.GroupByBox.Prompt = "将需要分组的列拖到此处";
  357. grid.DisplayLayout.Override.MaxSelectedRows = 0;
  358. grid.DisplayLayout.Override.SelectTypeRow = SelectType.Single;
  359. grid.DisplayLayout.Override.SelectTypeCol = SelectType.None;
  360. grid.DisplayLayout.Override.BorderStyleCell = UIElementBorderStyle.Solid;
  361. grid.DisplayLayout.Override.BorderStyleRow = UIElementBorderStyle.Solid;
  362. grid.DisplayLayout.Override.WrapHeaderText = DefaultableBoolean.False;
  363. grid.DisplayLayout.Override.DataErrorRowAppearance.BackColor = Color.Lime;
  364. grid.DisplayLayout.Override.SupportDataErrorInfo = SupportDataErrorInfo.RowsOnly;
  365. grid.DisplayLayout.Override.HeaderStyle = HeaderStyle.Standard;
  366. grid.DisplayLayout.Override.RowSelectorStyle = HeaderStyle.Standard;
  367. grid.DisplayLayout.Override.MinRowHeight = 22;
  368. //grid.DisplayLayout.Override.RowSelectorNumberStyle = RowSelectorNumberStyle.RowIndex;
  369. grid.DisplayLayout.Override.RowSelectorWidth = 35;
  370. grid.DisplayLayout.Override.CellClickAction = CellClickAction.EditAndSelectText;
  371. grid.DisplayLayout.Override.ActiveRowAppearance.FontData.Bold = DefaultableBoolean.True;
  372. grid.DisplayLayout.Override.ActiveRowAppearance.ForeColor = Color.Empty;
  373. grid.DisplayLayout.Override.ActiveRowAppearance.BackColor = Color.Empty;
  374. grid.DisplayLayout.Override.DataErrorRowAppearance.ForeColor = Color.Empty;
  375. grid.DisplayLayout.Override.DataErrorRowAppearance.BackColor = Color.Empty;
  376. grid.DisplayLayout.Override.RowAppearance.BackColor = Color.White;
  377. grid.DisplayLayout.Override.RowAppearance.BorderColor = Color.DarkGray;
  378. grid.DisplayLayout.Override.RowAppearance.TextVAlign = VAlign.Middle;
  379. grid.DisplayLayout.Override.RowAlternateAppearance.BackColor = Color.FromArgb(229, 255, 229);
  380. grid.DisplayLayout.Override.RowSelectorAppearance.TextVAlign = VAlign.Middle;
  381. grid.DisplayLayout.Override.HeaderAppearance.BackColor = Color.FromArgb(183, 208, 250);
  382. grid.DisplayLayout.Override.HeaderAppearance.BackColor2 = Color.FromArgb(183, 208, 250);
  383. grid.DisplayLayout.Override.RowSelectorAppearance.BackColor = Color.FromArgb(183, 208, 250);
  384. grid.DisplayLayout.Override.RowSelectorAppearance.BackColor2 = Color.FromArgb(183, 208, 250);
  385. }
  386. }
  387. }
  388. catch { }
  389. try
  390. {
  391. if (grid.DisplayLayout.Bands.Count > 1)
  392. {
  393. grid.DisplayLayout.Bands[1].Override.HeaderAppearance.BackColor = Color.FromArgb(233, 242, 199);
  394. grid.DisplayLayout.Bands[1].Override.HeaderAppearance.BackColor2 = Color.FromArgb(170, 184, 131);
  395. grid.DisplayLayout.Bands[1].Override.HeaderAppearance.BackGradientStyle = GradientStyle.Vertical;
  396. grid.DisplayLayout.Bands[1].Override.RowSelectorAppearance.BackColor = Color.FromArgb(233, 242, 199);
  397. grid.DisplayLayout.Bands[1].Override.RowSelectorAppearance.BackColor2 = Color.FromArgb(170, 184, 131);
  398. grid.DisplayLayout.Bands[1].Override.RowSelectorAppearance.BackGradientStyle = GradientStyle.Vertical;
  399. }
  400. }
  401. catch { }
  402. try
  403. {
  404. foreach (UltraGridBand band in grid.DisplayLayout.Bands)
  405. {
  406. band.Override.AllowRowLayoutCellSizing = RowLayoutSizing.None;
  407. band.Override.AllowRowLayoutCellSpanSizing = GridBagLayoutAllowSpanSizing.None;
  408. band.Override.AllowRowLayoutColMoving = GridBagLayoutAllowMoving.None;
  409. band.Override.AllowRowLayoutLabelSizing = (band.CardView ? RowLayoutSizing.None : RowLayoutSizing.Horizontal);
  410. band.Override.AllowRowLayoutLabelSpanSizing = GridBagLayoutAllowSpanSizing.None;
  411. }
  412. }
  413. catch { }
  414. try
  415. {
  416. grid.KeyDown += new KeyEventHandler(grid_KeyDown);
  417. }
  418. catch { }
  419. //平台中加了此事件
  420. try
  421. {
  422. grid.DoubleClickCell += new DoubleClickCellEventHandler(grid_DoubleClickCell);
  423. }
  424. catch { }
  425. //平台中加了此事件
  426. try
  427. {
  428. grid.BeforeRowsDeleted += new BeforeRowsDeletedEventHandler(grid_BeforeRowsDeleted);
  429. }
  430. catch { }
  431. if (AutoSizeUltraGrid && !grid.DisplayLayout.Bands[0].CardView)
  432. RefreshAndAutoSize(grid);
  433. }
  434. else
  435. {
  436. SetUltraGridStyle(ctrl, AutoSizeUltraGrid);
  437. }
  438. }
  439. catch (Exception ex)
  440. {
  441. Debug.Print(ex.Message);
  442. }
  443. }
  444. }
  445. public static void SetSelectColumnStyle(UltraGrid grid)
  446. {
  447. if (grid == null) return;
  448. foreach (UltraGridBand gb in grid.DisplayLayout.Bands)
  449. {
  450. if (gb.Columns.Exists("Selected"))
  451. {
  452. UltraGridColumn uc = gb.Columns["Selected"];
  453. uc.AllowGroupBy = Infragistics.Win.DefaultableBoolean.False;
  454. uc.CellClickAction = Infragistics.Win.UltraWinGrid.CellClickAction.Edit;
  455. uc.DataType = typeof(bool);
  456. uc.DefaultCellValue = false;
  457. uc.Header.Caption = "";
  458. uc.Header.CheckBoxVisibility = Infragistics.Win.UltraWinGrid.HeaderCheckBoxVisibility.Always;
  459. uc.RowLayoutColumnInfo.OriginX = 0;
  460. uc.RowLayoutColumnInfo.OriginY = 0;
  461. uc.RowLayoutColumnInfo.PreferredCellSize = new System.Drawing.Size(40, 0);
  462. uc.RowLayoutColumnInfo.SpanX = 1;
  463. uc.RowLayoutColumnInfo.SpanY = 1;
  464. uc.Style = Infragistics.Win.UltraWinGrid.ColumnStyle.CheckBox;
  465. }
  466. }
  467. }
  468. private static void grid_KeyDown(object sender, KeyEventArgs e)
  469. {
  470. try
  471. {
  472. UltraGrid grid = (UltraGrid)sender;
  473. if (grid == null) return;
  474. UltraGridCell cell = grid.ActiveCell;
  475. if (cell == null) return;
  476. bool bOK = false;
  477. int iRow = cell.Row.Index;
  478. int iColumn = cell.Column.Index;
  479. if (e.KeyCode == Keys.Up)
  480. {
  481. iRow--;
  482. if (iRow >= 0)
  483. bOK = true;
  484. }
  485. else if (e.KeyCode == Keys.Down)
  486. {
  487. iRow++;
  488. if (cell.Band.Index == 0)
  489. {
  490. if (iRow <= grid.Rows.Count - 1)
  491. bOK = true;
  492. }
  493. else
  494. {
  495. if (iRow <= cell.Row.ParentRow.ChildBands[cell.Band.Key].Rows.Count - 1)
  496. bOK = true;
  497. }
  498. }
  499. else if (e.KeyCode == Keys.Left)
  500. {
  501. grid.PerformAction(UltraGridAction.PrevCellByTab);
  502. }
  503. else if (e.KeyCode == Keys.Right)
  504. {
  505. grid.PerformAction(UltraGridAction.NextCellByTab);
  506. }
  507. if (bOK)
  508. {
  509. if (cell.Band.Index == 0)
  510. grid.ActiveCell = grid.Rows[iRow].Cells[iColumn];
  511. else
  512. grid.ActiveCell = cell.Row.ParentRow.ChildBands[cell.Band.Key].Rows[iRow].Cells[iColumn];
  513. grid.PerformAction(UltraGridAction.EnterEditMode);
  514. }
  515. }
  516. catch { }
  517. }
  518. private static void grid_DoubleClickCell(object sender, DoubleClickCellEventArgs e)
  519. {
  520. try
  521. {
  522. if (e.Cell.Row.HasChild())
  523. {
  524. if (e.Cell.Row.IsExpanded)
  525. e.Cell.Row.CollapseAll();
  526. else
  527. e.Cell.Row.ExpandAll();
  528. }
  529. }
  530. catch { }
  531. }
  532. private static void grid_BeforeRowsDeleted(object sender, BeforeRowsDeletedEventArgs e)
  533. {
  534. e.DisplayPromptMsg = false;
  535. }
  536. #endregion ultraGrid设置
  537. /// <summary>
  538. /// 设置UltraGrid指定列的ValueList
  539. /// </summary>
  540. /// <param name="myGrid"></param>
  541. /// <param name="strColumn"></param>
  542. /// <param name="vlist"></param>
  543. public static void SetGridValuelist(ref UltraGrid myGrid, string strColumn, ref ValueList vlist)
  544. {
  545. SetGridValuelist(ref myGrid, 0, strColumn, ref vlist);
  546. }
  547. /// <summary>
  548. /// 设置UltraGrid指定列的ValueList,指定Band
  549. /// </summary>
  550. /// <param name="myGrid"></param>
  551. /// <param name="BandIndex"></param>
  552. /// <param name="strColumn"></param>
  553. /// <param name="vlist"></param>
  554. public static void SetGridValuelist(ref UltraGrid myGrid, int BandIndex, string strColumn, ref ValueList vlist)
  555. {
  556. bool Contains = GridContainsColumn(myGrid, BandIndex, strColumn);
  557. try
  558. {
  559. if (Contains)
  560. {
  561. myGrid.DisplayLayout.Bands[BandIndex].Columns[strColumn].ValueList = vlist;
  562. //myGrid.DisplayLayout.Bands[BandIndex].Columns[strColumn].Style = Infragistics.Win.UltraWinGrid.ColumnStyle.DropDown;
  563. }
  564. }
  565. catch { }
  566. }
  567. /// <summary>
  568. /// 根据指定DataTable生成ValueList
  569. /// </summary>
  570. /// <param name="table"></param>
  571. /// <param name="strKey">value</param>
  572. /// <param name="strText">Text</param>
  573. /// <returns></returns>
  574. public static ValueList GeneralValuelist(ref DataTable table, string strKey, string strText)
  575. {
  576. if (table == null || !table.Columns.Contains(strKey) || !table.Columns.Contains(strText))
  577. return new ValueList();
  578. ArrayList alist = new ArrayList();
  579. ValueList vlist = new ValueList();
  580. for (int i = 0; i < table.Rows.Count; i++)
  581. {
  582. try
  583. {
  584. if (!alist.Contains(table.Rows[i][strKey]))
  585. {
  586. alist.Add(table.Rows[i][strKey]);
  587. vlist.ValueListItems.Add(table.Rows[i][strKey], Convert.ToString(table.Rows[i][strText]));
  588. }
  589. }
  590. catch { }
  591. }
  592. return vlist;
  593. }
  594. public static void InitUltraDataSource(UltraDataSource ultDataSource)
  595. {
  596. try
  597. {
  598. int iColumnsCount = ultDataSource.Band.Columns.Count;
  599. object[] obj = new object[iColumnsCount];
  600. if (ultDataSource.Rows.Count > 0) ultDataSource.Rows.Clear();
  601. for (int i = 0; i < ultDataSource.Band.Columns.Count; i++)
  602. {
  603. try
  604. {
  605. if (ultDataSource.Band.Columns[i].DataType == typeof(Bitmap) ||
  606. ultDataSource.Band.Columns[i].DataType == typeof(Image))
  607. obj[i] = null;
  608. else if (ultDataSource.Band.Columns[i].DataType == typeof(DateTime))
  609. obj[i] = DBNull.Value;
  610. else if (ultDataSource.Band.Columns[i].DataType == typeof(decimal) ||
  611. ultDataSource.Band.Columns[i].DataType == typeof(double) ||
  612. ultDataSource.Band.Columns[i].DataType == typeof(Single))
  613. obj[i] = 0;
  614. else
  615. obj[i] = "";
  616. }
  617. catch { }
  618. }
  619. ultDataSource.Rows.Add(obj);
  620. }
  621. catch { }
  622. }
  623. /// <summary>
  624. /// 初始化UltraDataSource
  625. /// </summary>
  626. /// <param name="ultDataSource"></param>
  627. public static void InitUserControl(UltraDataSource ultDataSource, UltraGrid ultraGrid)
  628. {
  629. try
  630. {
  631. InitUltraDataSource(ultDataSource);
  632. ultraGrid.UpdateData();
  633. }
  634. catch { }
  635. }
  636. /// <summary>
  637. /// UltraDataSource数据重置
  638. /// </summary>
  639. /// <param name="ultDataSource"></param>
  640. public static void ResetUltraDataSource(UltraDataSource ultDataSource)
  641. {
  642. try
  643. {
  644. if (ultDataSource.Rows.Count == 0)
  645. {
  646. InitUltraDataSource(ultDataSource);
  647. return;
  648. }
  649. for (int i = 0; i < ultDataSource.Band.Columns.Count; i++)
  650. {
  651. try
  652. {
  653. if (ultDataSource.Band.Columns[i].DataType == typeof(Bitmap) ||
  654. ultDataSource.Band.Columns[i].DataType == typeof(Image))
  655. ultDataSource.Rows[0][i] = null;
  656. else if (ultDataSource.Band.Columns[i].DataType == typeof(DateTime))
  657. ultDataSource.Rows[0][i] = DBNull.Value;
  658. else if (ultDataSource.Band.Columns[i].DataType == typeof(decimal) ||
  659. ultDataSource.Band.Columns[i].DataType == typeof(double) ||
  660. ultDataSource.Band.Columns[i].DataType == typeof(Single))
  661. ultDataSource.Rows[0][i] = 0;
  662. else
  663. ultDataSource.Rows[0][i] = "";
  664. }
  665. catch { }
  666. }
  667. }
  668. catch { }
  669. }
  670. /// <summary>
  671. /// UltraDataSource数据重置
  672. /// </summary>
  673. /// <param name="ultDataSource"></param>
  674. public static void ResetUltraDataSource(UltraDataSource ultDataSource, UltraGrid ultraGrid)
  675. {
  676. try
  677. {
  678. ResetUltraDataSource(ultDataSource);
  679. }
  680. catch { }
  681. finally
  682. {
  683. if (ultraGrid != null)
  684. ultraGrid.UpdateData();
  685. }
  686. }
  687. /// <summary>
  688. /// ultraGrid设置Cell只读属性
  689. /// </summary>
  690. /// <param name="ultraGrid"></param>
  691. /// <param name="bReadOnly"></param>
  692. public static void SetUltraGridCellReadOnly(ref UltraGrid ultraGrid, bool bReadOnly)
  693. {
  694. try
  695. {
  696. for (int i = 0; i < ultraGrid.DisplayLayout.Bands[0].Columns.Count; i++)
  697. {
  698. ultraGrid.DisplayLayout.Bands[0].Columns[i].CellActivation = (bReadOnly ? Activation.ActivateOnly : Activation.AllowEdit);
  699. ultraGrid.DisplayLayout.Bands[0].Columns[i].CellAppearance.BackColor = (bReadOnly ? Color.LightBlue : Color.White);
  700. }
  701. }
  702. catch { }
  703. }
  704. /// <summary>
  705. /// UltraDataSource是否包含strColumnName列
  706. /// </summary>
  707. /// <param name="ultDataSource"></param>
  708. /// <param name="strColumnName"></param>
  709. /// <returns></returns>
  710. public static bool UltraDataSourceContainsColumn(ref UltraDataSource ultDataSource, string strColumnName)
  711. {
  712. try
  713. {
  714. for (int i = 0; i < ultDataSource.Band.Columns.Count; i++)
  715. {
  716. if (ultDataSource.Band.Columns[i].Key == strColumnName)
  717. return true;
  718. }
  719. }
  720. catch { }
  721. return false;
  722. }
  723. public static bool GridContainsColumn(UltraGrid grid, string strColumn)
  724. {
  725. return GridContainsColumn(grid, 0, strColumn);
  726. }
  727. public static bool GridContainsColumn(UltraGridBand band, string strColumn)
  728. {
  729. for (int i = 0; i < band.Columns.Count; i++)
  730. {
  731. if (band.Columns[i].Key.Equals(strColumn))
  732. return true;
  733. }
  734. return false;
  735. }
  736. public static bool GridContainsColumn(UltraGrid grid, int BandIndex, string strColumn)
  737. {
  738. if (BandIndex <= grid.DisplayLayout.Bands.Count - 1)
  739. {
  740. for (int i = 0; i < grid.DisplayLayout.Bands[BandIndex].Columns.Count; i++)
  741. {
  742. if (grid.DisplayLayout.Bands[BandIndex].Columns[i].Key.Equals(strColumn))
  743. return true;
  744. }
  745. }
  746. return false;
  747. }
  748. public static void SetGridGroupBy(ref UltraGrid myGrid1, bool bAllowGroupBy)
  749. {
  750. try
  751. {
  752. if (!bAllowGroupBy)
  753. {
  754. for (int i = 0; i < myGrid1.DisplayLayout.Bands[0].Columns.Count; i++)
  755. {
  756. try
  757. {
  758. if (myGrid1.DisplayLayout.Bands[0].Columns[i].IsGroupByColumn)
  759. myGrid1.DisplayLayout.Bands[0].Columns[i].ResetIsGroupByColumn();
  760. }
  761. catch { }
  762. }
  763. }
  764. myGrid1.DisplayLayout.GroupByBox.Hidden = !bAllowGroupBy;
  765. }
  766. catch { }
  767. }
  768. public static void SetUltraGridRowFilter(ref UltraGrid myGrid1, bool bAllowFilter)
  769. {
  770. try
  771. {
  772. if (bAllowFilter)
  773. {
  774. if (myGrid1.DisplayLayout.Override.FilterUIType != FilterUIType.FilterRow)
  775. myGrid1.DisplayLayout.Override.FilterUIType = FilterUIType.FilterRow;
  776. for (int i = 0; i < myGrid1.DisplayLayout.Bands.Count; i++)
  777. {
  778. for (int j = 0; j < myGrid1.DisplayLayout.Bands[i].Columns.Count; j++)
  779. {
  780. try
  781. {
  782. if (!myGrid1.DisplayLayout.Bands[i].Columns[j].Hidden && myGrid1.DisplayLayout.Bands[i].Columns[j].RowLayoutColumnInfo.LabelPosition != LabelPosition.LabelOnly)
  783. {
  784. //string sKey = myGrid1.DisplayLayout.Bands[i].Columns[j].Key; //UNCK UNCH
  785. myGrid1.DisplayLayout.Bands[i].Columns[j].AllowRowFiltering = Infragistics.Win.DefaultableBoolean.True;
  786. }
  787. }
  788. catch { }
  789. }
  790. }
  791. }
  792. else
  793. {
  794. if (myGrid1.DisplayLayout.Override.FilterUIType != FilterUIType.HeaderIcons)
  795. myGrid1.DisplayLayout.Override.FilterUIType = FilterUIType.HeaderIcons;
  796. for (int i = 0; i < myGrid1.DisplayLayout.Bands.Count; i++)
  797. {
  798. try
  799. {
  800. myGrid1.DisplayLayout.Bands[i].ColumnFilters.ClearAllFilters();
  801. }
  802. catch { }
  803. for (int j = 0; j < myGrid1.DisplayLayout.Bands[i].Columns.Count; j++)
  804. {
  805. try
  806. {
  807. myGrid1.DisplayLayout.Bands[i].Columns[j].AllowRowFiltering = Infragistics.Win.DefaultableBoolean.False;
  808. }
  809. catch { }
  810. }
  811. }
  812. }
  813. }
  814. catch { }
  815. }
  816. public static void SetGridColumnsReadOnly(ref UltraGrid ultraGrid, ArrayList alistAllowEdit, ArrayList alistActivateOnly, ArrayList alistNoEdit, ArrayList alistDisabled)
  817. {
  818. try
  819. {
  820. for (int i = 0; i < ultraGrid.Rows.Count; i++)
  821. {
  822. if (alistAllowEdit != null && alistAllowEdit.Count > 0)
  823. {
  824. for (int j = 0; j < alistAllowEdit.Count; j++)
  825. {
  826. try
  827. {
  828. if (GridContainsColumn(ultraGrid, alistAllowEdit[j].ToString()))
  829. {
  830. ultraGrid.Rows[i].Cells[alistAllowEdit[j].ToString()].Activation = Activation.AllowEdit;
  831. }
  832. }
  833. catch (Exception ex)
  834. {
  835. Debug.WriteLine(ex.Message);
  836. }
  837. }
  838. }
  839. if (alistActivateOnly != null && alistActivateOnly.Count > 0)
  840. {
  841. for (int j = 0; j < alistActivateOnly.Count; j++)
  842. {
  843. try
  844. {
  845. if (GridContainsColumn(ultraGrid, alistActivateOnly[j].ToString()))
  846. {
  847. ultraGrid.Rows[i].Cells[alistActivateOnly[j].ToString()].Activation = Activation.ActivateOnly;
  848. }
  849. }
  850. catch (Exception ex)
  851. {
  852. Debug.WriteLine(ex.Message);
  853. }
  854. }
  855. }
  856. if (alistNoEdit != null && alistNoEdit.Count > 0)
  857. {
  858. for (int j = 0; j < alistNoEdit.Count; j++)
  859. {
  860. try
  861. {
  862. if (GridContainsColumn(ultraGrid, alistNoEdit[j].ToString()))
  863. {
  864. ultraGrid.Rows[i].Cells[alistNoEdit[j].ToString()].Activation = Activation.NoEdit;
  865. }
  866. }
  867. catch (Exception ex)
  868. {
  869. Debug.WriteLine(ex.Message);
  870. }
  871. }
  872. }
  873. if (alistDisabled != null && alistDisabled.Count > 0)
  874. {
  875. for (int j = 0; j < alistDisabled.Count; j++)
  876. {
  877. try
  878. {
  879. if (GridContainsColumn(ultraGrid, alistDisabled[j].ToString()))
  880. {
  881. ultraGrid.Rows[i].Cells[alistDisabled[j].ToString()].Activation = Activation.Disabled;
  882. }
  883. }
  884. catch (Exception ex)
  885. {
  886. Debug.WriteLine(ex.Message);
  887. }
  888. }
  889. }
  890. }
  891. }
  892. catch (Exception ex)
  893. {
  894. Debug.WriteLine(ex.Message);
  895. }
  896. }
  897. public static bool HasCheckedRows(ref UltraGrid ultraGrid, string strColumnCondition, object value, string strOutColumn, out ArrayList alistValue, out string strMsg)
  898. {
  899. strMsg = "";
  900. alistValue = new ArrayList();
  901. try
  902. {
  903. if (ultraGrid == null || ultraGrid.Rows.Count == 0)
  904. {
  905. return false;
  906. }
  907. if (!string.IsNullOrEmpty(strColumnCondition))
  908. {
  909. if (!GridContainsColumn(ultraGrid, 0, strColumnCondition))
  910. {
  911. strMsg = "不存在条件字段【" + strColumnCondition + "】!";
  912. return false;
  913. }
  914. }
  915. if (string.IsNullOrEmpty(strOutColumn))
  916. {
  917. strMsg = "未指定输出字段【" + strOutColumn + "】!";
  918. return false;
  919. }
  920. if (!GridContainsColumn(ultraGrid, 0, strOutColumn))
  921. {
  922. strMsg = "不存在输出字段【" + strOutColumn + "】!";
  923. return false;
  924. }
  925. for (int i = 0; i < ultraGrid.Rows.Count; i++)
  926. {
  927. if (!string.IsNullOrEmpty(strColumnCondition))
  928. {
  929. if (ultraGrid.Rows[i].Cells[strColumnCondition].Value.Equals(value))
  930. {
  931. if (ultraGrid.Rows[i].Cells["FCFLAG"].Value.ToString() != "校磅")
  932. alistValue.Add(ultraGrid.Rows[i].Cells[strOutColumn].Value);
  933. }
  934. }
  935. else
  936. {
  937. if (ultraGrid.Rows[i].Cells["FCFLAG"].Value.ToString() != "校磅")
  938. alistValue.Add(ultraGrid.Rows[i].Cells[strOutColumn].Value);
  939. }
  940. }
  941. return true;
  942. }
  943. catch (Exception ex)
  944. {
  945. strMsg = ex.Message;
  946. alistValue = null;
  947. }
  948. return false;
  949. }
  950. private static UltraGridExcelExporter ultraGridExcelExporter1 = new UltraGridExcelExporter();
  951. /// <summary>
  952. /// 判断系统是否安装EXCEL组件
  953. /// </summary>
  954. /// <returns></returns>
  955. public static bool ExcelInstalled()
  956. {
  957. bool bInstalled = (Type.GetTypeFromProgID("Excel.Application") != null);
  958. if (!bInstalled)
  959. {
  960. MessageBox.Show("系统没有安装EXCEL组件,无法导出数据!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
  961. }
  962. return bInstalled;
  963. }
  964. public static void ExportDataWithSaveDialog(ref UltraGrid ultraGrid1, string strFileName)
  965. {
  966. try
  967. {
  968. if (ultraGrid1.Rows.Count == 0) return;
  969. if (strFileName.Length == 0)
  970. strFileName = "未命名";
  971. SaveFileDialog dlg = new SaveFileDialog();
  972. dlg.Title = "保存";
  973. dlg.OverwritePrompt = true;
  974. dlg.Filter = "Excel文件(*.xls)|*.xls";
  975. dlg.AddExtension = true;
  976. dlg.FileName = strFileName;
  977. string strCaption = strFileName;
  978. if (dlg.ShowDialog() == DialogResult.OK)
  979. {
  980. strFileName = dlg.FileName;
  981. Workbook wb = new Workbook();
  982. Worksheet ws2 = wb.Worksheets.Add("Sheet1");
  983. WorksheetMergedCellsRegion region = null;
  984. ws2.Rows[0].Cells[0].Value = strCaption;
  985. ws2.Rows[0].Cells[0].CellFormat.Alignment = Infragistics.Excel.HorizontalCellAlignment.Center;
  986. ws2.Rows[0].Cells[0].CellFormat.VerticalAlignment = Infragistics.Excel.VerticalCellAlignment.Center;
  987. ws2.Rows[0].Cells[0].CellFormat.WrapText = Infragistics.Excel.ExcelDefaultableBoolean.True;
  988. ws2.Rows[0].Cells[0].CellFormat.Font.Name = "宋体";
  989. ws2.Rows[0].Cells[0].CellFormat.Font.Height = 350;
  990. ws2.Rows[0].Cells[0].CellFormat.Font.Bold = ExcelDefaultableBoolean.True;
  991. ws2.Rows[0].Height = 880;
  992. int iMaxY = 0;
  993. int iMaxHeight = 0;
  994. int iMaxWidth = 0;
  995. for (int i = 0; i < ultraGrid1.DisplayLayout.Bands[0].Columns.Count; i++)
  996. {
  997. if (!ultraGrid1.DisplayLayout.Bands[0].Columns[i].Hidden && ultraGrid1.DisplayLayout.Bands[0].Columns[i].RowLayoutColumnInfo.LabelPosition != LabelPosition.None)
  998. {
  999. try
  1000. {
  1001. ws2.Rows[1 + ultraGrid1.DisplayLayout.Bands[0].Columns[i].RowLayoutColumnInfo.OriginY].Cells[ultraGrid1.DisplayLayout.Bands[0].Columns[i].RowLayoutColumnInfo.OriginX].Value = ultraGrid1.DisplayLayout.Bands[0].Columns[i].Header.Caption;
  1002. ws2.Rows[1 + ultraGrid1.DisplayLayout.Bands[0].Columns[i].RowLayoutColumnInfo.OriginY].Cells[ultraGrid1.DisplayLayout.Bands[0].Columns[i].RowLayoutColumnInfo.OriginX].CellFormat.Alignment = Infragistics.Excel.HorizontalCellAlignment.Center;
  1003. ws2.Rows[1 + ultraGrid1.DisplayLayout.Bands[0].Columns[i].RowLayoutColumnInfo.OriginY].Cells[ultraGrid1.DisplayLayout.Bands[0].Columns[i].RowLayoutColumnInfo.OriginX].CellFormat.VerticalAlignment = Infragistics.Excel.VerticalCellAlignment.Center;
  1004. ws2.Rows[1 + ultraGrid1.DisplayLayout.Bands[0].Columns[i].RowLayoutColumnInfo.OriginY].Cells[ultraGrid1.DisplayLayout.Bands[0].Columns[i].RowLayoutColumnInfo.OriginX].CellFormat.WrapText = Infragistics.Excel.ExcelDefaultableBoolean.True;
  1005. ws2.Rows[1 + ultraGrid1.DisplayLayout.Bands[0].Columns[i].RowLayoutColumnInfo.OriginY].Cells[ultraGrid1.DisplayLayout.Bands[0].Columns[i].RowLayoutColumnInfo.OriginX].CellFormat.Font.Bold = Infragistics.Excel.ExcelDefaultableBoolean.True;
  1006. ws2.Rows[1 + ultraGrid1.DisplayLayout.Bands[0].Columns[i].RowLayoutColumnInfo.OriginY].Cells[ultraGrid1.DisplayLayout.Bands[0].Columns[i].RowLayoutColumnInfo.OriginX].CellFormat.WrapText = Infragistics.Excel.ExcelDefaultableBoolean.True;
  1007. ws2.Rows[1 + ultraGrid1.DisplayLayout.Bands[0].Columns[i].RowLayoutColumnInfo.OriginY].Cells[ultraGrid1.DisplayLayout.Bands[0].Columns[i].RowLayoutColumnInfo.OriginX].CellFormat.Font.Name = "宋体";
  1008. region = ws2.MergedCellsRegions.Add(1 + ultraGrid1.DisplayLayout.Bands[0].Columns[i].RowLayoutColumnInfo.OriginY,
  1009. ultraGrid1.DisplayLayout.Bands[0].Columns[i].RowLayoutColumnInfo.OriginX,
  1010. 1 + ultraGrid1.DisplayLayout.Bands[0].Columns[i].RowLayoutColumnInfo.OriginY +
  1011. ultraGrid1.DisplayLayout.Bands[0].Columns[i].RowLayoutColumnInfo.SpanY - 1,
  1012. ultraGrid1.DisplayLayout.Bands[0].Columns[i].RowLayoutColumnInfo.OriginX +
  1013. ultraGrid1.DisplayLayout.Bands[0].Columns[i].RowLayoutColumnInfo.SpanX - 1);
  1014. //region.CellFormat.FillPatternForegroundColor = Color.Lime;
  1015. //region.CellFormat.LeftBorderColor = region.CellFormat.RightBorderColor = region.CellFormat.TopBorderColor = region.CellFormat.BottomBorderColor = Color.DarkBlue;
  1016. if (ws2.Rows[1 + ultraGrid1.DisplayLayout.Bands[0].Columns[i].RowLayoutColumnInfo.OriginY].Height != 500)
  1017. ws2.Rows[1 + ultraGrid1.DisplayLayout.Bands[0].Columns[i].RowLayoutColumnInfo.OriginY].Height = 500;
  1018. if (ultraGrid1.DisplayLayout.Bands[0].Columns[i].RowLayoutColumnInfo.SpanY > iMaxHeight)
  1019. {
  1020. iMaxHeight = ultraGrid1.DisplayLayout.Bands[0].Columns[i].RowLayoutColumnInfo.SpanY;
  1021. }
  1022. if (ultraGrid1.DisplayLayout.Bands[0].Columns[i].RowLayoutColumnInfo.OriginY > iMaxY)
  1023. {
  1024. iMaxY = ultraGrid1.DisplayLayout.Bands[0].Columns[i].RowLayoutColumnInfo.OriginY;
  1025. iMaxHeight = iMaxY + ultraGrid1.DisplayLayout.Bands[0].Columns[i].RowLayoutColumnInfo.SpanY;
  1026. }
  1027. if (ultraGrid1.DisplayLayout.Bands[0].Columns[i].RowLayoutColumnInfo.SpanX + ultraGrid1.DisplayLayout.Bands[0].Columns[i].RowLayoutColumnInfo.OriginX > iMaxWidth)
  1028. {
  1029. iMaxWidth = ultraGrid1.DisplayLayout.Bands[0].Columns[i].RowLayoutColumnInfo.SpanX + ultraGrid1.DisplayLayout.Bands[0].Columns[i].RowLayoutColumnInfo.OriginX;
  1030. }
  1031. }
  1032. catch (Exception ex) { string str = ex.Message; }
  1033. }
  1034. }
  1035. region = ws2.MergedCellsRegions.Add(0, 0, 0, iMaxWidth - 1);
  1036. //region.CellFormat.FillPatternForegroundColor = Color.Lime;
  1037. //region.CellFormat.LeftBorderColor = region.CellFormat.RightBorderColor = region.CellFormat.TopBorderColor = region.CellFormat.BottomBorderColor = Color.DarkBlue;
  1038. decimal dValue = 0.0M;
  1039. bool bDecimal = false;
  1040. for (int i = 0; i < ultraGrid1.Rows.Count; i++)
  1041. {
  1042. for (int j = 0; j < ultraGrid1.DisplayLayout.Bands[0].Columns.Count; j++)
  1043. {
  1044. try
  1045. {
  1046. if (!ultraGrid1.DisplayLayout.Bands[0].Columns[j].Hidden && ultraGrid1.DisplayLayout.Bands[0].Columns[j].RowLayoutColumnInfo.LabelPosition != LabelPosition.LabelOnly)
  1047. {
  1048. if (decimal.TryParse(Convert.ToString(ultraGrid1.Rows[i].Cells[j].Text), out dValue))
  1049. {
  1050. if (dValue == 0 || dValue < 1 || !Convert.ToString(ultraGrid1.Rows[i].Cells[j].Text).StartsWith("0"))
  1051. {
  1052. bDecimal = true;
  1053. }
  1054. else
  1055. {
  1056. bDecimal = false;
  1057. }
  1058. }
  1059. else
  1060. {
  1061. bDecimal = false;
  1062. }
  1063. if (bDecimal)
  1064. {
  1065. ws2.Rows[1 + iMaxHeight + i].Cells[ultraGrid1.DisplayLayout.Bands[0].Columns[j].RowLayoutColumnInfo.OriginX].Value = dValue;
  1066. }
  1067. else
  1068. {
  1069. ws2.Rows[1 + iMaxHeight + i].Cells[ultraGrid1.DisplayLayout.Bands[0].Columns[j].RowLayoutColumnInfo.OriginX].Value = ultraGrid1.Rows[i].Cells[j].Text;
  1070. }
  1071. //if (ws2.Rows[1 + iMaxHeight + i].Height != 320)
  1072. // ws2.Rows[1 + iMaxHeight + i].Height = 320;
  1073. //ws2.Rows[1 + iMaxHeight + i].Cells[ultraGrid1.DisplayLayout.Bands[0].Columns[j].RowLayoutColumnInfo.OriginX].CellFormat.VerticalAlignment = Infragistics.Excel.VerticalCellAlignment.Center;
  1074. //if (ultraGrid1.DisplayLayout.Bands[0].Columns[j].DataType == typeof(System.Decimal))
  1075. //{
  1076. // ws2.Rows[1 + iMaxHeight + i].Cells[ultraGrid1.DisplayLayout.Bands[0].Columns[j].RowLayoutColumnInfo.OriginX].CellFormat.Alignment = Infragistics.Excel.HorizontalCellAlignment.Right;
  1077. //}
  1078. //region = ws2.MergedCellsRegions.Add(1 + iMaxHeight + i, ultraGrid1.DisplayLayout.Bands[0].Columns[j].RowLayoutColumnInfo.OriginX,
  1079. // 1 + iMaxHeight + i, ultraGrid1.DisplayLayout.Bands[0].Columns[j].RowLayoutColumnInfo.OriginX);
  1080. //region.CellFormat.LeftBorderColor = region.CellFormat.RightBorderColor = region.CellFormat.TopBorderColor = region.CellFormat.BottomBorderColor = Color.DarkBlue;
  1081. }
  1082. }
  1083. catch { }
  1084. }
  1085. }
  1086. wb.Save(strFileName);
  1087. if (File.Exists(strFileName) &&
  1088. MessageBox.Show("数据导出成功!\n需要打开所导出文件吗?", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
  1089. {
  1090. ProcessStartInfo p = new ProcessStartInfo(strFileName);
  1091. p.WorkingDirectory = Path.GetDirectoryName(strFileName);
  1092. Process.Start(p);
  1093. }
  1094. }
  1095. }
  1096. catch { }
  1097. }
  1098. public static void ExportDataWithSaveDialog2(ref UltraGrid myGrid1, string strFileName)
  1099. {
  1100. try
  1101. {
  1102. if (myGrid1.Rows.Count == 0) return;
  1103. if (strFileName.Length == 0) strFileName = "未命名";
  1104. SaveFileDialog dlg = new SaveFileDialog();
  1105. dlg.Title = "保存";
  1106. dlg.OverwritePrompt = true;
  1107. dlg.Filter = "Excel文件(*.xls)|*.xls";
  1108. dlg.AddExtension = true;
  1109. dlg.FileName = strFileName;
  1110. if (dlg.ShowDialog() == DialogResult.OK)
  1111. {
  1112. strFileName = dlg.FileName;
  1113. ultraGridExcelExporter1.Export(myGrid1, strFileName);
  1114. //if (MessageBox.Show("数据导出成功!\n需要打开所导出文件吗?", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
  1115. //{
  1116. // ProcessStartInfo p = new ProcessStartInfo(strFileName);
  1117. // p.WorkingDirectory = Path.GetDirectoryName(strFileName);
  1118. // Process.Start(p);
  1119. //}
  1120. }
  1121. }
  1122. catch (Exception ex)
  1123. {
  1124. MessageBox.Show(ex.Message);
  1125. }
  1126. }
  1127. public static bool IPCheck(string IP)
  1128. {
  1129. string Segment = @"(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)";
  1130. return Regex.IsMatch(IP, ("^" + Segment + @"\." + Segment + @"\." + Segment + @"\." + Segment + "$"));
  1131. }
  1132. public static bool Pinging(string IP)
  1133. {
  1134. try
  1135. {
  1136. Ping ping = new Ping();
  1137. PingOptions options = new PingOptions();
  1138. options.DontFragment = true;
  1139. byte[] buffer = Encoding.ASCII.GetBytes("TEST PING");
  1140. PingReply reply = ping.Send(IP, 3000, buffer, options);
  1141. if (reply.Status == IPStatus.Success)
  1142. {
  1143. return true;
  1144. }
  1145. }
  1146. catch { }
  1147. return false;
  1148. }
  1149. public static bool PrinterExists(string strPrinterName)
  1150. {
  1151. if (string.IsNullOrEmpty(strPrinterName))
  1152. return false;
  1153. foreach (string printerName in PrinterSettings.InstalledPrinters)
  1154. {
  1155. if (strPrinterName.Equals(printerName))
  1156. return true;
  1157. }
  1158. return false;
  1159. }
  1160. public static bool FontFamilyNameExists(string strFontFamilyName)
  1161. {
  1162. if (string.IsNullOrEmpty(strFontFamilyName))
  1163. return false;
  1164. foreach (FontFamily fontfamily in FontFamily.Families)
  1165. {
  1166. if (strFontFamilyName.Equals(fontfamily.Name))
  1167. return true;
  1168. }
  1169. return false;
  1170. }
  1171. public static void ResetHashtable(ref Hashtable htbl)
  1172. {
  1173. if (htbl == null)
  1174. htbl = new Hashtable();
  1175. else
  1176. htbl.Clear();
  1177. }
  1178. public static void ResetArraylist(ref ArrayList alist)
  1179. {
  1180. if (alist == null)
  1181. alist = new ArrayList();
  1182. else
  1183. alist.Clear();
  1184. }
  1185. public static bool CheckComboBoxSelection(UltraComboEditor cbEdit, string Caption, bool MustSelect, bool ShowError, out string Message)
  1186. {
  1187. Message = "";
  1188. try
  1189. {
  1190. if (cbEdit != null)
  1191. {
  1192. if (MustSelect)
  1193. {
  1194. if (cbEdit.SelectedIndex < 0)
  1195. {
  1196. Message = cbEdit.Text.Trim().Length > 0 ? Caption + "不正确,请从列表中选取!" : "请选择" + Caption + "!";
  1197. if (ShowError)
  1198. MessageBox.Show(Message, "提示", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
  1199. return false;
  1200. }
  1201. }
  1202. else
  1203. {
  1204. if (cbEdit.SelectedIndex < 0 && cbEdit.Text.Trim().Length > 0)
  1205. {
  1206. Message = Caption + "不正确,请从列表中选取!";
  1207. if (ShowError)
  1208. MessageBox.Show(Message, "提示", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
  1209. return false;
  1210. }
  1211. }
  1212. }
  1213. return true;
  1214. }
  1215. catch (Exception ex)
  1216. {
  1217. Message = Caption + "验证出错!\n" + ex.Message;
  1218. if (ShowError)
  1219. MessageBox.Show(Message, "提示", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
  1220. return false;
  1221. }
  1222. }
  1223. #region 窗体及控件样式
  1224. private static Color _BACKCOLOR = Color.FromArgb(202, 222, 247);
  1225. private static void SetControlAppearance(Control ctrl)
  1226. {
  1227. try
  1228. {
  1229. if (ctrl.GetType().IsSubclassOf(typeof(Form)))
  1230. ctrl.BackColor = _BACKCOLOR;
  1231. else if (ctrl.GetType().Equals(typeof(Panel)))
  1232. {
  1233. ctrl.BackColor = _BACKCOLOR;
  1234. ((Panel)ctrl).BorderStyle = BorderStyle.None;
  1235. }
  1236. else if (ctrl.GetType().Equals(typeof(PictureBox)))
  1237. {
  1238. ctrl.BackColor = _BACKCOLOR;
  1239. ((PictureBox)ctrl).BorderStyle = BorderStyle.None;
  1240. }
  1241. else if (ctrl.GetType().Equals(typeof(GroupBox)))
  1242. ctrl.BackColor = _BACKCOLOR;
  1243. else if (ctrl.GetType().Equals(typeof(ToolStrip)))
  1244. ctrl.BackColor = _BACKCOLOR;
  1245. else if (ctrl.GetType().IsSubclassOf(typeof(Label)))
  1246. ctrl.BackColor = _BACKCOLOR;
  1247. else if (ctrl.GetType().IsSubclassOf(typeof(TextBoxBase)))
  1248. ctrl.BackColor = SystemColors.Window;
  1249. else if (ctrl.GetType().IsSubclassOf(typeof(UserControl)))
  1250. ctrl.BackColor = _BACKCOLOR;
  1251. if (ctrl.GetType().IsSubclassOf(typeof(ControlBase)) && !ctrl.GetType().IsSubclassOf(typeof(EditorButtonControlBase)))
  1252. ((ControlBase)ctrl).Appearance.BackColor = _BACKCOLOR;
  1253. else if (ctrl.GetType().Equals(typeof(UltraGroupBox)) || ctrl.GetType().IsSubclassOf(typeof(UltraGroupBox)))
  1254. {
  1255. ((UltraGroupBox)ctrl).ViewStyle = GroupBoxViewStyle.Office2007;
  1256. ((UltraGroupBox)ctrl).HeaderBorderStyle = UIElementBorderStyle.Etched;
  1257. ((UltraGroupBox)ctrl).BorderStyle = GroupBoxBorderStyle.None;
  1258. ((UltraGroupBox)ctrl).Appearance.BackColor = _BACKCOLOR;
  1259. ((UltraGroupBox)ctrl).Appearance.BackColor2 = _BACKCOLOR;
  1260. ((UltraGroupBox)ctrl).HeaderAppearance.BackColor = Color.FromArgb(183, 208, 250);
  1261. ((UltraGroupBox)ctrl).HeaderAppearance.BackColor2 = Color.FromArgb(183, 208, 250);
  1262. ((UltraGroupBox)ctrl).ContentAreaAppearance.BackColor = Color.FromArgb(218, 232, 248);
  1263. ((UltraGroupBox)ctrl).ContentAreaAppearance.BackColor2 = Color.FromArgb(218, 232, 248);
  1264. }
  1265. else if (ctrl.GetType().Equals(typeof(UltraPanel)) || ctrl.GetType().IsSubclassOf(typeof(UltraPanel)))
  1266. {
  1267. ((UltraPanel)ctrl).Appearance.BackColor = _BACKCOLOR;
  1268. ((UltraPanel)ctrl).Appearance.BackColor2 = _BACKCOLOR;
  1269. ((UltraPanel)ctrl).Appearance.BorderColor = Color.DarkGray;
  1270. ((UltraPanel)ctrl).BorderStyle = UIElementBorderStyle.Etched;
  1271. }
  1272. else if (ctrl.GetType().Equals(typeof(UltraTabControl)))
  1273. {
  1274. ((UltraTabControl)ctrl).ViewStyle = Infragistics.Win.UltraWinTabControl.ViewStyle.Office2007;
  1275. }
  1276. }
  1277. catch { }
  1278. }
  1279. public static void SetControlStyle(Control parentCtrl)
  1280. {
  1281. try
  1282. {
  1283. SetControlAppearance(parentCtrl);
  1284. foreach (Control ctrl in parentCtrl.Controls)
  1285. {
  1286. if (ctrl is UserControl)
  1287. continue;
  1288. SetControlStyle(ctrl);
  1289. }
  1290. }
  1291. catch { }
  1292. }
  1293. #endregion 窗体及控件样式
  1294. public static void SetUltraGridColumnColor(ValueList colorList, string columnName, UltraGrid ultraGrid1)
  1295. {
  1296. try
  1297. {
  1298. Infragistics.Win.Appearance apprc;
  1299. OperatorCondition oc;
  1300. ConditionValueAppearance cva = new ConditionValueAppearance();
  1301. for (int i = 0; i < colorList.ValueListItems.Count; i++)
  1302. {
  1303. apprc = new Infragistics.Win.Appearance();
  1304. if (i == 0)
  1305. apprc.BackColor = Color.Cornsilk;
  1306. else if (i == 1)
  1307. apprc.BackColor = Color.Gainsboro;
  1308. else if (i == 2)
  1309. apprc.BackColor = Color.LightSkyBlue;
  1310. else if (i == 3)
  1311. apprc.BackColor = Color.PaleGreen;
  1312. else if (i == 4)
  1313. apprc.BackColor = Color.Lime;
  1314. oc = new OperatorCondition(ConditionOperator.Equals, colorList.ValueListItems[0].DisplayText, false, typeof(string));
  1315. cva.Add((ICondition)oc, apprc);
  1316. }
  1317. ultraGrid1.DisplayLayout.Bands[0].Columns[columnName].ValueBasedAppearance = cva;
  1318. }
  1319. catch { }
  1320. }
  1321. public static void SetStaticsInfoCount(ref UltraGrid myGrid1, ArrayList alistColumns, bool clearExists)
  1322. {
  1323. try
  1324. {
  1325. UltraGridBand band = myGrid1.DisplayLayout.Bands[0];
  1326. if (clearExists)
  1327. band.Summaries.Clear();
  1328. band.Override.SummaryFooterCaptionVisible = Infragistics.Win.DefaultableBoolean.False;
  1329. for (int i = 0; i < alistColumns.Count; i++)
  1330. {
  1331. try
  1332. {
  1333. SummarySettings summary = band.Summaries.Add(SummaryType.Count, band.Columns[alistColumns[i].ToString()]);
  1334. summary.SummaryDisplayArea = SummaryDisplayAreas.BottomFixed;
  1335. summary.SummaryPosition = SummaryPosition.UseSummaryPositionColumn;
  1336. summary.DisplayFormat = "{0}";
  1337. summary.Appearance.TextHAlign = Infragistics.Win.HAlign.Right;
  1338. summary.Appearance.TextVAlign = Infragistics.Win.VAlign.Middle;
  1339. summary.Appearance.FontData.Bold = DefaultableBoolean.True;
  1340. }
  1341. catch { }
  1342. }
  1343. }
  1344. catch { }
  1345. }
  1346. public static void SetStaticsInfoSum(ref UltraGrid myGrid1, ArrayList alistColumns, bool clearExists)
  1347. {
  1348. try
  1349. {
  1350. UltraGridBand band = myGrid1.DisplayLayout.Bands[0];
  1351. if (clearExists)
  1352. band.Summaries.Clear();
  1353. band.Override.SummaryFooterCaptionVisible = Infragistics.Win.DefaultableBoolean.False;
  1354. for (int i = 0; i < alistColumns.Count; i++)
  1355. {
  1356. try
  1357. {
  1358. SummarySettings summary = band.Summaries.Add(SummaryType.Sum, band.Columns[alistColumns[i].ToString()]);
  1359. summary.SummaryDisplayArea = SummaryDisplayAreas.BottomFixed;
  1360. summary.SummaryPosition = SummaryPosition.UseSummaryPositionColumn;
  1361. summary.DisplayFormat = "{0}";
  1362. summary.Appearance.TextHAlign = Infragistics.Win.HAlign.Right;
  1363. summary.Appearance.TextVAlign = Infragistics.Win.VAlign.Middle;
  1364. summary.Appearance.FontData.Bold = DefaultableBoolean.True;
  1365. }
  1366. catch { }
  1367. }
  1368. }
  1369. catch { }
  1370. }
  1371. public static void ClearUltraGridColumnsSort(UltraGrid ultraGrid)
  1372. {
  1373. if (ultraGrid != null)
  1374. {
  1375. foreach (UltraGridBand band in ultraGrid.DisplayLayout.Bands)
  1376. {
  1377. foreach (UltraGridColumn col in band.Columns)
  1378. {
  1379. if (col.SortIndicator == SortIndicator.Disabled)
  1380. continue;
  1381. col.SortIndicator = SortIndicator.None;
  1382. }
  1383. }
  1384. }
  1385. }
  1386. public static DataTable ConvertToDataTable(IList list)
  1387. {
  1388. DataTable table = new DataTable();
  1389. if ((list != null) && (list.Count > 0))
  1390. {
  1391. Hashtable hashtable2;
  1392. Hashtable hashtable = (Hashtable)list[0];
  1393. foreach (DictionaryEntry entry in hashtable)
  1394. {
  1395. string columnName = entry.Key.ToString();
  1396. if (entry.Value == null)
  1397. {
  1398. table.Columns.Add(columnName, typeof(string));
  1399. }
  1400. else if ((entry.Value != null) && typeof(Hashtable).Equals(entry.Value.GetType()))
  1401. {
  1402. hashtable2 = (Hashtable)entry.Value;
  1403. table.Columns.Add(columnName, hashtable2["value"].GetType());
  1404. }
  1405. else
  1406. {
  1407. table.Columns.Add(columnName, entry.Value.GetType());
  1408. }
  1409. }
  1410. for (int i = 0; i < list.Count; i++)
  1411. {
  1412. hashtable = (Hashtable)list[i];
  1413. ArrayList list2 = new ArrayList();
  1414. foreach (DictionaryEntry entry in hashtable)
  1415. {
  1416. if ((entry.Value != null) && typeof(Hashtable).Equals(entry.Value.GetType()))
  1417. {
  1418. hashtable2 = (Hashtable)entry.Value;
  1419. list2.Add(hashtable2["value"]);
  1420. }
  1421. else
  1422. {
  1423. list2.Add(entry.Value);
  1424. }
  1425. }
  1426. table.LoadDataRow(list2.ToArray(), true);
  1427. }
  1428. }
  1429. return table;
  1430. }
  1431. /// <summary>
  1432. /// 冻结行
  1433. /// </summary>
  1434. /// <param name="ultraGrid1"></param>
  1435. /// <param name="column"></param>
  1436. public static void UseFixedHeader(UltraGrid ultraGrid1, string column)
  1437. {
  1438. ultraGrid1.DisplayLayout.UseFixedHeaders = true;
  1439. ultraGrid1.DisplayLayout.Bands[0].Columns[column].Header.Fixed = true;
  1440. ultraGrid1.DisplayLayout.Bands[0].Columns[column].Header.FixOnRight = DefaultableBoolean.True;
  1441. ultraGrid1.DisplayLayout.Bands[0].Columns[column].Header.FixedHeaderIndicator = FixedHeaderIndicator.None;
  1442. ultraGrid1.DisplayLayout.Override.FixedHeaderAppearance.BackColor = Color.LightYellow;
  1443. ultraGrid1.DisplayLayout.Override.FixedCellAppearance.BackColor = Color.LightYellow;
  1444. ultraGrid1.DisplayLayout.Override.FixedCellSeparatorColor = Color.Red;
  1445. }
  1446. /// <summary>
  1447. /// 设置ultraGrid为不可编辑,如有选择Checked列Key为selectkey
  1448. /// </summary>
  1449. /// <param name="ulgrid">ultraGrid控件</param>
  1450. public static void SetNoEnable(params Infragistics.Win.UltraWinGrid.UltraGrid[] ulgrids)
  1451. {
  1452. try
  1453. {
  1454. foreach (var ulgrid in ulgrids)
  1455. {
  1456. foreach (Infragistics.Win.UltraWinGrid.UltraGridBand band in ulgrid.DisplayLayout.Bands)
  1457. {
  1458. band.Override.CellClickAction = CellClickAction.EditAndSelectText;//选中整行
  1459. foreach (Infragistics.Win.UltraWinGrid.UltraGridColumn Col in band.Columns)
  1460. {
  1461. Col.CellClickAction = CellClickAction.EditAndSelectText;
  1462. if (Col.Style != Infragistics.Win.UltraWinGrid.ColumnStyle.CheckBox && Col.Key.ToLower() != "selectkey" && Col.Key.ToLower() != "check" && Col.Key.ToLower() != "isselect" && Col.Key.ToLower() != "checkbox")//选择复选框
  1463. {
  1464. if ((Col.Tag + "") == "AllowEdit") continue;
  1465. Col.CellActivation = Infragistics.Win.UltraWinGrid.Activation.ActivateOnly;
  1466. //Col.SortIndicator = SortIndicator.Disabled;//不可以排序;会导致抬头变两行
  1467. if (Col.DataType == typeof(DateTime?))
  1468. {
  1469. Col.Style = Infragistics.Win.UltraWinGrid.ColumnStyle.DateTime;
  1470. }
  1471. }
  1472. else
  1473. {
  1474. Col.CellClickAction = CellClickAction.Edit;
  1475. Col.CellActivation = Infragistics.Win.UltraWinGrid.Activation.AllowEdit;
  1476. }
  1477. }
  1478. }
  1479. }
  1480. }
  1481. catch
  1482. {
  1483. }
  1484. }
  1485. /// <summary>
  1486. /// 设置ultraGrid为不可编辑,strKey为例外可以编辑的列
  1487. /// </summary>
  1488. /// <param name="ulgrid">ultraGrid控件</param>
  1489. public static void SetNoEnable(UltraGrid ulgrid, List<string> strkey)
  1490. {
  1491. try
  1492. {
  1493. foreach (Infragistics.Win.UltraWinGrid.UltraGridBand band in ulgrid.DisplayLayout.Bands)
  1494. {
  1495. band.Override.CellClickAction = CellClickAction.EditAndSelectText;//选中整行
  1496. foreach (Infragistics.Win.UltraWinGrid.UltraGridColumn Col in band.Columns)
  1497. {
  1498. Col.CellClickAction = CellClickAction.EditAndSelectText;
  1499. if (Col.Style != Infragistics.Win.UltraWinGrid.ColumnStyle.CheckBox && !strkey.Exists(s => s.ToLower() == Col.Key.ToLower()))//选择复选框
  1500. {
  1501. Col.CellActivation = Infragistics.Win.UltraWinGrid.Activation.ActivateOnly;
  1502. //Col.SortIndicator = SortIndicator.Disabled;//不可以排序;会导致抬头变两行
  1503. if (Col.DataType == typeof(DateTime?))
  1504. {
  1505. Col.Style = Infragistics.Win.UltraWinGrid.ColumnStyle.DateTime;
  1506. }
  1507. }
  1508. else
  1509. {
  1510. Col.CellClickAction = CellClickAction.Edit;
  1511. Col.CellActivation = Infragistics.Win.UltraWinGrid.Activation.AllowEdit;
  1512. }
  1513. }
  1514. }
  1515. }
  1516. catch
  1517. {
  1518. }
  1519. }
  1520. public static void ShowGridCaption<T>(UltraGridBand gridBand)
  1521. {
  1522. try
  1523. {
  1524. PropertyInfo[] propertyInfos = typeof(T).GetProperties();
  1525. foreach (PropertyInfo propertyInfo in propertyInfos)
  1526. {
  1527. if (gridBand.Columns.Exists(propertyInfo.Name))
  1528. {
  1529. //设置单元格为空时的值。
  1530. if (propertyInfo.PropertyType == typeof(string))
  1531. {
  1532. gridBand.Columns[propertyInfo.Name].Nullable = Infragistics.Win.UltraWinGrid.Nullable.EmptyString;
  1533. }
  1534. object[] desAttributes = propertyInfo.GetCustomAttributes(typeof(DescriptionAttribute), false);
  1535. if (desAttributes.Length > 0)
  1536. {
  1537. gridBand.Columns[propertyInfo.Name].Header.Caption = ((DescriptionAttribute)desAttributes[0]).Description;
  1538. }
  1539. }
  1540. }
  1541. }
  1542. catch (Exception ex)
  1543. {
  1544. throw ex;
  1545. }
  1546. }
  1547. }
  1548. }