求教,如何在winform里面将表头进行合并??急!!!!!!!
例如 a | b | c |d|e
注意:是表头哈.用datagridview控件可以做到么?非要自己画? --------------------编程问答-------------------- http://singlepine.cnblogs.com/archive/2005/11/23/282990.html --------------------编程问答-------------------- 你这个是DataGrid控件,我想问问datagridview控件能否做到? --------------------编程问答-------------------- 1.功能说明:
将连续的多个列合并成一个新列。
2.不足之处:
不能合并多层。比如下图这样的功能是没有的。
3.使用参考.
在form的构造函数里写下如下代码
1. Utility.exGridView.isEnLarged = false;
在datagridview的cellpaiting事件中写如下代码
1. Utility.exGridView exG = new Utility.exGridView();
2. List colNameCollection=new List();
3. for (int i = 0; i < 10; i++)
4. {
5. //"colDraw"+i.ToString()是columnName的属性值
6. colNameCollection.Add("colDraw" + i.ToString());
7. }
8. exG.MergeHeader(sender, e, colNameCollection, "0-9中奖号码分布图");
4.效果截图
5.源文件(没找到添加附件的地方,就贴出代码了)
1. using System;
2. using System.Collections.Generic;
3. using System.Text;
4. using System.Windows.Forms;
5. using System.Drawing;
6.
7. namespace Utility
8. {
9. public class exGridView
10. {
11. #region 合并列时使用到的位置和大小属性
12. int cTop = 0;//被合并表头区域的顶部坐标
13. int cLeft = 0;//被合并表头区域的左边坐标
14. /// <summary>
15. /// 被合并表头区域的宽
16. /// </summary>
17. int cWidth = 0;
18. int cHeight = 0;//。。。高
19. #endregion
20. /// <summary>
21. /// 判断是否已经将datagridview的表头变高了,只增高一次。
22. /// </summary>
23. public static bool isEnLarged = false;
24.
25. /// <summary>
26. /// 合并表头,用在dataGridView的CellPainting事件中。
27. /// </summary>
28. /// <param name="sender">需要重绘的dataGridview</param>
29. /// <param name="e">CellPainting中的参数</param>
30. ///<param name="colName">列的集合(列必须是连续的,第一列放在最前面)</param>
31. /// <param name="headerText">列合并后显示的文本</param>
32. public void MergeHeader(object sender, DataGridViewCellPaintingEventArgs e,List<string> colNameCollection,string headerText)
33. {
34. if (e.RowIndex == -1)
35. {
36. DataGridView dataGridView1=sender as DataGridView;
37. string colName = dataGridView1.Columns[e.ColumnIndex].Name;
38. if (!isEnLarged)
39. {
40. //0.扩展表头高度为当前的2倍
41. dataGridView1.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.EnableResizing;
42. dataGridView1.ColumnHeadersHeight = e.CellBounds.Height * 2;
43. isEnLarged = true;
44. }
45. if (colNameCollection.Contains(colName))
46. {
47. #region 重绘列头
48. //1.计算colLen个列的区域
49. if (colNameCollection.IndexOf(colName)==0)
50. {
51. cTop = e.CellBounds.Top;
52. cLeft = e.CellBounds.Left;
53.
54. cWidth = e.CellBounds.Width;
55. cHeight = e.CellBounds.Height/2;
56.
57. foreach(string colNameItem in colNameCollection)
58. {
59. if (colNameItem.Equals(colName))
60. {//除去自己一个,加了之后colLen-1个列的宽
61. continue;
62. }
63. cWidth += dataGridView1.Columns[colNameItem].Width;
64. }
65. }
66.
67. Rectangle cArea = new Rectangle(cLeft, cTop, cWidth, cHeight);
68. //2.把区域设置为背景色,没有列的分线及任何文字。
69. using (Brush backColorBrush = new SolidBrush(e.CellStyle.BackColor))
70. {
71. e.Graphics.FillRectangle(backColorBrush, cArea);
72.
73. }
74. //3.绘制新列头的边框
75. using (Pen gridPen = new Pen(dataGridView1.GridColor))
76. {
77. //3.1 上部边框
78. e.Graphics.DrawLine(gridPen, cLeft, cTop, cLeft + cWidth, cTop);
79. using (Pen hilightPen = new Pen(Color.WhiteSmoke))
80. {
81. //3.2 顶部高光
82. e.Graphics.DrawLine(hilightPen, cLeft, cTop + 1, cLeft + cWidth, cTop + 1);
83. //3.3 左部反光线
84. e.Graphics.DrawLine(hilightPen, cLeft, cTop + 3, cLeft, cTop + cHeight - 2);
85. }
86. //3.4 下部边框
87. e.Graphics.DrawLine(gridPen, cLeft, cTop + cHeight - 1, cLeft + cWidth, cTop + cHeight - 1);
88. //3.5 右部边框
89. e.Graphics.DrawLine(gridPen, cLeft + cWidth - 1, cTop, cLeft + cWidth - 1, cTop + cHeight);//(cTop+cHeight)/2);
90. }
91. //4.写文本
92. if (colNameCollection.IndexOf(colName) == 0)
93. {//不是第一列则不写文字。
94. int wHeadStr = (int)(headerText.Length * e.CellStyle.Font.SizeInPoints);
95. int wHeadCell = cWidth;
96. int pHeadLeft = (wHeadCell - wHeadStr) / 2 - 6;
97. using (Brush foreBrush = new SolidBrush(e.CellStyle.ForeColor))
98. {
99. e.Graphics.DrawString(headerText, e.CellStyle.Font, foreBrush, new PointF(cLeft + pHeadLeft, cTop + 3));
100. }
101. }
102.
103.
104. //5 绘制子列背景
105. int FatherColHeight = e.CellBounds.Height / 2;//上面一行的高度
106. using (Brush backColorBrush = new SolidBrush(e.CellStyle.BackColor))
107. {
108. e.Graphics.FillRectangle(backColorBrush, new Rectangle(e.CellBounds.X, e.CellBounds.Y + FatherColHeight, e.CellBounds.Width - 1, e.CellBounds.Height / 2 - 1));
109. }
110. //5.1绘制子列的边框
111. using (Pen gridPen = new Pen(dataGridView1.GridColor))
112. {
113. using (Pen hilightPen = new Pen(Color.WhiteSmoke))
114. {
115. //5.2 左部反光线
116. e.Graphics.DrawLine(hilightPen, cLeft, cTop + 3 + FatherColHeight, cLeft, cTop + cHeight - 2 + FatherColHeight);
117. }
118. //5.3 下部边框
119. e.Graphics.DrawLine(gridPen, cLeft, cTop + cHeight - 1 + FatherColHeight, cLeft + cWidth, cTop + cHeight - 1 + FatherColHeight);
120.
121. //5.4 右部边框
122. e.Graphics.DrawLine(gridPen, e.CellBounds.X + e.CellBounds.Width - 1, e.CellBounds.Top + FatherColHeight, e.CellBounds.X + e.CellBounds.Width - 1, e.CellBounds.Top + e.CellBounds.Height + FatherColHeight);//(cTop+cHeight)/2);
123.
124. }
125. //5.5 写子列的文本
126. int wStr = (int)(dataGridView1.Columns[e.ColumnIndex].HeaderText.Length * e.CellStyle.Font.SizeInPoints);
127. int wCell = e.CellBounds.Width;
128. int pLeft = (wCell - wStr) / 2;//相对CELL左边框的左坐标
129.
130. using (Brush foreBrush = new SolidBrush(e.CellStyle.ForeColor))
131. {
132. e.Graphics.DrawString(dataGridView1.Columns[e.ColumnIndex].HeaderText, e.CellStyle.Font, foreBrush, new PointF(e.CellBounds.X + pLeft, cTop + 3 + FatherColHeight));
133. }
134. #endregion
135. e.Handled = true;
136. }
137. }
138. }
139. }
140. } --------------------编程问答--------------------
--------------------编程问答--------------------
public class exGridView
{
#region 合并列时使用到的位置和大小属性
int cTop = 0;//被合并表头区域的顶部坐标
int cLeft = 0;//被合并表头区域的左边坐标
/// <summary>
/// 被合并表头区域的宽
/// </summary>
int cWidth = 0;
int cHeight = 0;//。。。高
#endregion
/// <summary>
/// 判断是否已经将datagridview的表头变高了,只增高一次。
/// </summary>
public static bool isEnLarged = false;
/// <summary>
/// 合并表头,用在dataGridView的CellPainting事件中。
/// </summary>
/// <param name="sender">需要重绘的dataGridview</param>
/// <param name="e">CellPainting中的参数</param>
///<param name="colName">列的集合(列必须是连续的,第一列放在最前面)</param>
/// <param name="headerText">列合并后显示的文本</param>
public void MergeHeader(object sender, DataGridViewCellPaintingEventArgs e, List<string> colNameCollection, string headerText)
{
if (e.RowIndex == -1)
{
DataGridView dataGridView1 = sender as DataGridView;
string colName = dataGridView1.Columns[e.ColumnIndex].Name;
if (!isEnLarged)
{
//0.扩展表头高度为当前的2倍
dataGridView1.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.EnableResizing;
dataGridView1.ColumnHeadersHeight = e.CellBounds.Height * 2;
isEnLarged = true;
}
if (colNameCollection.Contains(colName))
{
#region 重绘列头
//1.计算colLen个列的区域
if (colNameCollection.IndexOf(colName) == 0)
{
cTop = e.CellBounds.Top;
cLeft = e.CellBounds.Left;
cWidth = e.CellBounds.Width;
cHeight = e.CellBounds.Height / 2;
foreach (string colNameItem in colNameCollection)
{
if (colNameItem.Equals(colName))
{//除去自己一个,加了之后colLen-1个列的宽
continue;
}
cWidth += dataGridView1.Columns[colNameItem].Width;
}
}
Rectangle cArea = new Rectangle(cLeft, cTop, cWidth, cHeight);
//2.把区域设置为背景色,没有列的分线及任何文字。
using (Brush backColorBrush = new SolidBrush(e.CellStyle.BackColor))
{
e.Graphics.FillRectangle(backColorBrush, cArea);
}
//3.绘制新列头的边框
using (Pen gridPen = new Pen(dataGridView1.GridColor))
{
//3.1 上部边框
e.Graphics.DrawLine(gridPen, cLeft, cTop, cLeft + cWidth, cTop);
using (Pen hilightPen = new Pen(Color.WhiteSmoke))
{
//3.2 顶部高光
e.Graphics.DrawLine(hilightPen, cLeft, cTop + 1, cLeft + cWidth, cTop + 1);
//3.3 左部反光线
e.Graphics.DrawLine(hilightPen, cLeft, cTop + 3, cLeft, cTop + cHeight - 2);
}
//3.4 下部边框
e.Graphics.DrawLine(gridPen, cLeft, cTop + cHeight - 1, cLeft + cWidth, cTop + cHeight - 1);
//3.5 右部边框
e.Graphics.DrawLine(gridPen, cLeft + cWidth - 1, cTop, cLeft + cWidth - 1, cTop + cHeight);//(cTop+cHeight)/2);
}
//4.写文本
if (colNameCollection.IndexOf(colName) == 0)
{//不是第一列则不写文字。
int wHeadStr = (int)(headerText.Length * e.CellStyle.Font.SizeInPoints);
int wHeadCell = cWidth;
int pHeadLeft = (wHeadCell - wHeadStr) / 2 - 6;
using (Brush foreBrush = new SolidBrush(e.CellStyle.ForeColor))
{
e.Graphics.DrawString(headerText, e.CellStyle.Font, foreBrush, new PointF(cLeft + pHeadLeft, cTop + 3));
}
}
//5 绘制子列背景
int FatherColHeight = e.CellBounds.Height / 2;//上面一行的高度
using (Brush backColorBrush = new SolidBrush(e.CellStyle.BackColor))
{
e.Graphics.FillRectangle(backColorBrush, new Rectangle(e.CellBounds.X, e.CellBounds.Y + FatherColHeight, e.CellBounds.Width - 1, e.CellBounds.Height / 2 - 1));
}
//5.1绘制子列的边框
using (Pen gridPen = new Pen(dataGridView1.GridColor))
{
using (Pen hilightPen = new Pen(Color.WhiteSmoke))
{
//5.2 左部反光线
e.Graphics.DrawLine(hilightPen, cLeft, cTop + 3 + FatherColHeight, cLeft, cTop + cHeight - 2 + FatherColHeight);
}
//5.3 下部边框
e.Graphics.DrawLine(gridPen, cLeft, cTop + cHeight - 1 + FatherColHeight, cLeft + cWidth, cTop + cHeight - 1 + FatherColHeight);
//5.4 右部边框
e.Graphics.DrawLine(gridPen, e.CellBounds.X + e.CellBounds.Width - 1, e.CellBounds.Top + FatherColHeight, e.CellBounds.X + e.CellBounds.Width - 1, e.CellBounds.Top + e.CellBounds.Height + FatherColHeight);//(cTop+cHeight)/2);
}
//5.5 写子列的文本
int wStr = (int)(dataGridView1.Columns[e.ColumnIndex].HeaderText.Length * e.CellStyle.Font.SizeInPoints);
int wCell = e.CellBounds.Width;
int pLeft = (wCell - wStr) / 2;//相对CELL左边框的左坐标
using (Brush foreBrush = new SolidBrush(e.CellStyle.ForeColor))
{
e.Graphics.DrawString(dataGridView1.Columns[e.ColumnIndex].HeaderText, e.CellStyle.Font, foreBrush, new PointF(e.CellBounds.X + pLeft, cTop + 3 + FatherColHeight));
}
#endregion
e.Handled = true;
}
}
}
}
补充:.NET技术 , C#