EntityGrid.cs 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Drawing;
  5. using System.Data;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Windows.Forms;
  9. using Infragistics.Win.UltraWinGrid;
  10. using System.Reflection;
  11. using Infragistics.Shared;
  12. namespace Core.Mes.Client.Comm.Control
  13. {
  14. [ComplexBindingProperties("DataSource", "DataMember")]
  15. [DefaultEvent("InitializeLayout")]
  16. [DefaultProperty("DisplayLayout")]
  17. [Designer("Infragistics.Win.UltraWinGrid.Design.UltraGridDesigner, Infragistics2.Win.v9.2.Design, Version=9.2.20092.1003, Culture=neutral, PublicKeyToken=7dd5c3163f2cd0cb")]
  18. [LicenseProvider(typeof(UltraLicenseProvider))]
  19. [ToolboxBitmap(typeof(UltraGridBase), "ToolboxBitmaps.CLR2.UltraGrid.bmp")]
  20. public partial class EntityGrid : UltraGrid
  21. {
  22. public EntityGrid()
  23. {
  24. InitializeComponent();
  25. }
  26. private int initCount = 0;
  27. private bool IsDesignMode()
  28. {
  29. if (System.ComponentModel.LicenseManager.UsageMode == System.ComponentModel.LicenseUsageMode.Designtime)
  30. {
  31. return true;
  32. }
  33. else if (System.Diagnostics.Process.GetCurrentProcess().ProcessName.ToUpper().Equals("DEVENV"))
  34. {
  35. return true;
  36. }
  37. return false;
  38. }
  39. protected override void OnInitializeLayout(InitializeLayoutEventArgs e)
  40. {
  41. base.OnInitializeLayout(e);
  42. if (IsDesignMode())
  43. {
  44. if (initCount == 0)
  45. {
  46. initCount++;
  47. SetEntityGridCaption();
  48. }
  49. }
  50. else
  51. {
  52. this.DisplayLayout.Override.AllowRowLayoutCellSizing = RowLayoutSizing.None;
  53. this.DisplayLayout.Override.SelectedRowAppearance.BackColor = Color.Empty;
  54. this.DisplayLayout.Override.ActiveRowAppearance.BackColor = Color.Empty;
  55. this.DisplayLayout.Override.ActiveRowAppearance.ForeColor = Color.Empty;
  56. }
  57. }
  58. private void SetEntityGridCaption()
  59. {
  60. if (this.DataSource != null && this.DataSource is BindingSource)
  61. {
  62. try
  63. {
  64. BindingSource bindSouce = (BindingSource)this.DataSource;
  65. Type type = (Type)bindSouce.DataSource;
  66. SetCaption(this.DisplayLayout.Bands[0], type);
  67. if (this.DisplayLayout.Bands.Count > 1)
  68. {
  69. System.Reflection.PropertyInfo[] pis = type.GetProperties();
  70. foreach (System.Reflection.PropertyInfo pi in pis)
  71. {
  72. if (pi.PropertyType.IsGenericType)
  73. {
  74. Type genericType = pi.PropertyType.GetGenericArguments()[0];
  75. SetCaption(this.DisplayLayout.Bands[1], genericType);
  76. }
  77. }
  78. }
  79. }
  80. catch (Exception ex)
  81. {
  82. }
  83. }
  84. }
  85. private void SetCaption(UltraGridBand gridBand, Type type)
  86. {
  87. //this.BeginUpdate();
  88. try
  89. {
  90. PropertyInfo[] propertyInfos = type.GetProperties();
  91. foreach (PropertyInfo propertyInfo in propertyInfos)
  92. {
  93. if (gridBand.Columns.Exists(propertyInfo.Name))
  94. {
  95. //设置单元格为空时的值。
  96. if (propertyInfo.PropertyType == typeof(string)
  97. && gridBand.Columns[propertyInfo.Name].Nullable != Infragistics.Win.UltraWinGrid.Nullable.EmptyString)
  98. {
  99. gridBand.Columns[propertyInfo.Name].Nullable = Infragistics.Win.UltraWinGrid.Nullable.EmptyString;
  100. }
  101. object[] desAttributes = propertyInfo.GetCustomAttributes(typeof(DescriptionAttribute), false);
  102. if (desAttributes.Length > 0)
  103. {
  104. if (gridBand.Columns[propertyInfo.Name].Header.Caption != ((DescriptionAttribute)desAttributes[0]).Description)
  105. {
  106. gridBand.Columns[propertyInfo.Name].Header.Caption = ((DescriptionAttribute)desAttributes[0]).Description;
  107. }
  108. }
  109. }
  110. }
  111. }
  112. finally
  113. {
  114. //this.EndUpdate();
  115. }
  116. }
  117. private UltraGrid grid = new UltraGrid();
  118. public UltraGrid StyleGrid
  119. {
  120. get { return grid; }
  121. set { grid = value; }
  122. }
  123. }
  124. }