.net WinForm DataGridView 多表头处理方案一则

.net WinForm DataGridView 多表头处理方案一则

  思路:重画,重载
  实现:
我是使用重载表格控件来处理的代码如下:

表格初始化时:
            _mergedManager = new DataGridViewHeadsMergedManager(this);

使用代码:
技巧,在属性弹出的数据窗口回车确认后,会自动添加一行表头,任意多行,先横向,后纵向自动合并;-)

 1 ExpandedBlockStart.gif       #region 多表头
 2InBlock.gif
 3InBlock.gif        [
 4InBlock.gifCategory("GridX"),
 5InBlock.gifDescription("设置辅助标题"),
 6InBlock.gif]
 7InBlock.gif        public int ColumnTitleHeight
 8ExpandedSubBlockStart.gif        {
 9InBlock.gif            get
10ExpandedSubBlockStart.gif            {
11InBlock.gif                return _mergedManager.ColumnTitleHeight;
12InBlock.gif
13ExpandedSubBlockEnd.gif            }

14InBlock.gif            set
15ExpandedSubBlockStart.gif            {
16InBlock.gif
17InBlock.gif                _mergedManager.ColumnTitleHeight = value;
18InBlock.gif
19ExpandedSubBlockEnd.gif            }

20InBlock.gif
21ExpandedSubBlockEnd.gif        }

22InBlock.gif        [
23InBlock.gifCategory("GridX"),
24InBlock.gifDescription("设置辅助标题"),
25InBlock.gif]
26InBlock.gif        public string[] TitleHeads
27ExpandedSubBlockStart.gif        {
28InBlock.gif
29InBlock.gif            get
30ExpandedSubBlockStart.gif            {
31InBlock.gif
32InBlock.gif                if (this._mergedManager == null)
33InBlock.gif                    this._mergedManager = new DataGridViewHeadsMergedManager(this);
34InBlock.gif
35InBlock.gif                return _mergedManager.TitleHeads;
36InBlock.gif
37InBlock.gif
38InBlock.gif
39ExpandedSubBlockEnd.gif            }

40InBlock.gif            set
41ExpandedSubBlockStart.gif            {
42InBlock.gif
43InBlock.gif
44InBlock.gif                _mergedManager.TitleHeads = value;
45InBlock.gif
46ExpandedSubBlockEnd.gif            }

47InBlock.gif
48ExpandedSubBlockEnd.gif        }

49InBlock.gif
50InBlock.gif        public void ClearHeads()
51ExpandedSubBlockStart.gif        {
52InBlock.gif            _mergedManager.ClearHeads();
53InBlock.gif            //this.ScrollBars = ScrollBars.Both;
54ExpandedSubBlockEnd.gif        }

55InBlock.gif
56InBlock.gif        private void InternalDisposeHeads()
57ExpandedSubBlockStart.gif        {
58InBlock.gif            _mergedManager.Dispose();
59ExpandedSubBlockEnd.gif        }

60InBlock.gif
61InBlock.gif        public void AppendNewHead(params string[] Caption)
62ExpandedSubBlockStart.gif        {
63InBlock.gif            //this.ScrollBars = ScrollBars.Vertical;   
64InBlock.gif            _mergedManager.AppendNewHead(Caption);
65InBlock.gif
66ExpandedSubBlockEnd.gif        }

67InBlock.gif
68InBlock.gif        public void HeadRemoveAt(int index)
69ExpandedSubBlockStart.gif        {
70InBlock.gif            _mergedManager.HeadRemoveAt(index);
71InBlock.gif
72ExpandedSubBlockEnd.gif        }

73InBlock.gif
74InBlock.gif        public int HeadCount
75ExpandedSubBlockStart.gif        {
76InBlock.gif            get
77ExpandedSubBlockStart.gif            {
78InBlock.gif                return _mergedManager.HeadCount;
79ExpandedSubBlockEnd.gif            }

80InBlock.gif
81ExpandedSubBlockEnd.gif        }

82ExpandedBlockEnd.gif        #endregion

83 None.gif
84 None.gif
85 None.gif
86 None.gif

表头代码:
略...


Demo:
先右键定义6个DataGridViewColumn列,我第二列设不可见
再在TitleHeads窗口中输
Column1,Column1,Column1,Column1,Column2,Column2
Column1,Column1,Column3,Column5,Column5,Column5
Column1,Column2,Column3,Column4,Column7,Column8
Column1,Column2,Column3,Column4,Column7,Column6
代码:
            this.grid.TitleHeads = new string[] {
        "Column1,Column1,Column1,Column1,Column2,Column2",
        "Column1,Column1,Column3,Column5,Column5,Column5",
        "Column1,Column2,Column3,Column4,Column7,Column8",
        "Column1,Column2,Column3,Column4,Column7,Column6"};
贴图:
.net WinForm DataGridView 多表头处理方案一则_第1张图片

你可能感兴趣的:(winform,.net,string,null)