.net WinForm DataGridView 多表头处理方案一则
思路:重画,重载
实现:
我是使用重载表格控件来处理的代码如下:
表格初始化时:
_mergedManager = new DataGridViewHeadsMergedManager(this);
使用代码:
技巧,在属性弹出的数据窗口回车确认后,会自动添加一行表头,任意多行,先横向,后纵向自动合并;-)
1
多表头
#region 多表头
2
3 [
4Category("GridX"),
5Description("设置辅助标题"),
6]
7 public int ColumnTitleHeight
8 {
9 get
10 {
11 return _mergedManager.ColumnTitleHeight;
12
13 }
14 set
15 {
16
17 _mergedManager.ColumnTitleHeight = value;
18
19 }
20
21 }
22 [
23Category("GridX"),
24Description("设置辅助标题"),
25]
26 public string[] TitleHeads
27 {
28
29 get
30 {
31
32 if (this._mergedManager == null)
33 this._mergedManager = new DataGridViewHeadsMergedManager(this);
34
35 return _mergedManager.TitleHeads;
36
37
38
39 }
40 set
41 {
42
43
44 _mergedManager.TitleHeads = value;
45
46 }
47
48 }
49
50 public void ClearHeads()
51 {
52 _mergedManager.ClearHeads();
53 //this.ScrollBars = ScrollBars.Both;
54 }
55
56 private void InternalDisposeHeads()
57 {
58 _mergedManager.Dispose();
59 }
60
61 public void AppendNewHead(params string[] Caption)
62 {
63 //this.ScrollBars = ScrollBars.Vertical;
64 _mergedManager.AppendNewHead(Caption);
65
66 }
67
68 public void HeadRemoveAt(int index)
69 {
70 _mergedManager.HeadRemoveAt(index);
71
72 }
73
74 public int HeadCount
75 {
76 get
77 {
78 return _mergedManager.HeadCount;
79 }
80
81 }
82 #endregion
83
84
85
86
表头代码:
略...
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"};
贴图: