FrmSelectColumnDisplay.cs 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Text;
  7. using System.Linq;
  8. using System.Threading.Tasks;
  9. using System.Windows.Forms;
  10. using DevExpress.XtraEditors;
  11. using ProjectBase.Controls.Others;
  12. using DevExpress.XtraGrid.Views.Grid;
  13. using DevExpress.XtraGrid.Columns;
  14. namespace ProjectBase.Controls
  15. {
  16. public partial class FrmSelectColumnDisplay : DevExpress.XtraEditors.XtraForm
  17. {
  18. private CheckedListBox mCheckedListBox;
  19. private GridView mDataGridView;
  20. /// <summary>
  21. /// 显示数据的DataGridView对象
  22. /// </summary>
  23. public GridView DataGridView
  24. {
  25. get { return mDataGridView; }
  26. set { mDataGridView = value; }
  27. }
  28. /// <summary>
  29. /// 显示的列名称(按顺序排列)
  30. /// </summary>
  31. public string DisplayColumNames
  32. {
  33. get;
  34. set;
  35. }
  36. /// <summary>
  37. /// 别名对照字典
  38. /// </summary>
  39. public Dictionary<string, string> ColumnNameAlias
  40. {
  41. get;
  42. set;
  43. }
  44. private int MaxHeight = 300;
  45. private int DisplayWidth = 200;
  46. private bool isSelectAll = false;
  47. public FrmSelectColumnDisplay()
  48. {
  49. InitializeComponent();
  50. }
  51. /// <summary>
  52. /// 获取真正的列名称
  53. /// </summary>
  54. /// <param name="columnName">可能大小写不一样的列名</param>
  55. /// <returns></returns>
  56. private string GetRightColumnName(string columnName)
  57. {
  58. foreach (GridColumn c in mDataGridView.Columns)
  59. {
  60. if (c.FieldName.Equals(columnName, StringComparison.OrdinalIgnoreCase))
  61. return c.FieldName;
  62. }
  63. return "";
  64. }
  65. private string GetColumnNameAlias(string name)
  66. {
  67. if (ColumnNameAlias.ContainsKey(name.ToUpper()))
  68. {
  69. return ColumnNameAlias[name.ToUpper()];
  70. }
  71. else
  72. {
  73. return name;
  74. }
  75. }
  76. private void Init()
  77. {
  78. mCheckedListBox = new CheckedListBox();
  79. mCheckedListBox.CheckOnClick = true;
  80. mCheckedListBox.ItemCheck += new ItemCheckEventHandler(mCheckedListBox_ItemCheck);
  81. mCheckedListBox.Items.Clear();
  82. foreach (string columnName in DisplayColumNames.Split(new char[] { '|', ',' }))
  83. {
  84. string newName = GetRightColumnName(columnName);
  85. if (!string.IsNullOrEmpty(newName))
  86. {
  87. mCheckedListBox.Items.Add(new CListItem(GetColumnNameAlias(newName), newName), true);
  88. }
  89. }
  90. int PreferredHeight = (mCheckedListBox.Items.Count * 16) + 7;
  91. mCheckedListBox.Height = (PreferredHeight < MaxHeight) ? PreferredHeight : MaxHeight;
  92. mCheckedListBox.Width = this.DisplayWidth;
  93. mCheckedListBox.Dock = DockStyle.Fill;
  94. this.panel1.Controls.Clear();
  95. this.panel1.Controls.Add(mCheckedListBox);
  96. }
  97. void mCheckedListBox_ItemCheck(object sender, ItemCheckEventArgs e)
  98. {
  99. CListItem item = this.mCheckedListBox.Items[e.Index] as CListItem;
  100. if (item != null)
  101. {
  102. mDataGridView.Columns[item.Value].Visible = (e.NewValue == CheckState.Checked);
  103. }
  104. //mDataGridView.Columns[e.Index].Visible = (e.NewValue == CheckState.Checked);
  105. }
  106. private void FrmSelectColumnDisplay_Load(object sender, EventArgs e)
  107. {
  108. Init();
  109. }
  110. private void check_SelectAll_CheckedChanged(object sender, EventArgs e)
  111. {
  112. isSelectAll = check_SelectAll.Checked;
  113. for (int i = 0; i < this.mCheckedListBox.Items.Count; i++)
  114. {
  115. this.mCheckedListBox.SetItemChecked(i, isSelectAll);
  116. }
  117. }
  118. private void check_Inverse_CheckedChanged(object sender, EventArgs e)
  119. {
  120. for (int i = 0; i < this.mCheckedListBox.Items.Count; i++)
  121. {
  122. this.mCheckedListBox.SetItemChecked(i, !this.mCheckedListBox.GetItemChecked(i));
  123. }
  124. }
  125. }
  126. }