ClsControlPack.cs 75 KB

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