利用CellPainting事件
方法说明:先将原始表格的单元格框线清除,再利用事件对需要显示的框线进行重新绘制,达到单元格合并的效果。
int index = 0; // 用于得到合并后文字应该显示的位置,这里是第一行第二列开始每四列合并一次,
// 第二行第二列开始每两列合并一次
// D1为DataGridView的name属性
private void D1_CellPainting(object sender, DataGridViewCellPaintingEventArgs e)
{
string text = string.Empty; // 用于存放合并后需要居中显示的文字
// e.RowIndex <= 1,即对第一行和第二行重新处理
if (e.RowIndex <= 1 && e.ColumnIndex <= D1.Columns.Count - 1 && e.ColumnIndex > 0)
{
using
(
Brush gridBrush = new SolidBrush(this.D1.GridColor),
backColorBrush = new SolidBrush(e.CellStyle.BackColor)
)
{
using (Pen gridLinePen = new Pen(gridBrush))
{
// 清除单元格
e.Graphics.FillRectangle(backColorBrush, e.CellBounds);
// 如果下一行和当前行的数据不同,则在当前的单元格画一条底边线
try
{
// 如果前一个单元格与后一个单元格不同,则画上右边线
if (e.ColumnIndex <= D1.Columns.Count - 2 &&
D1.Rows[e.RowIndex].Cells[e.ColumnIndex].Value != null &&
D1.Rows[e.RowIndex].Cells[e.ColumnIndex + 1].Value.ToString() != e.Value.ToString())
{
e.Graphics.DrawLine(gridLinePen, e.CellBounds.Right - 1,
e.CellBounds.Top, e.CellBounds.Right - 1,
e.CellBounds.Bottom);
}
}
catch { }
// 画底边线
if (e.ColumnIndex <= D1.Columns.Count - 1)
{
e.Graphics.DrawLine(gridLinePen, e.CellBounds.Left,
e.CellBounds.Bottom - 1, e.CellBounds.Right - 1,
e.CellBounds.Bottom - 1);
// 绘制顶部的线
if (e.RowIndex == 0)
e.Graphics.DrawLine(gridLinePen, e.CellBounds.Right - 1,
e.CellBounds.Top, e.CellBounds.Left,
e.CellBounds.Top);
}
// 补画表头最右边界
if (e.ColumnIndex == D1.Columns.Count - 1)
{
e.Graphics.DrawLine(gridLinePen, e.CellBounds.Right - 1,
e.CellBounds.Top, e.CellBounds.Right - 1,
e.CellBounds.Bottom);
}
if (e.Value != null)
{
if (e.ColumnIndex < D1.Columns.Count - 1 && D1.Rows[e.RowIndex].Cells[e.ColumnIndex + 1].Value.ToString() == e.Value.ToString())
{
index++;
}
else
{
text = e.Value.ToString();
int left = e.CellBounds.Left, top = e.CellBounds.Top,
right = e.CellBounds.Right, bottom = e.CellBounds.Bottom;
SolidBrush fontBrush = new SolidBrush(e.CellStyle.ForeColor);
if (e.CellStyle.Alignment == DataGridViewContentAlignment.MiddleCenter)
{
if (e.RowIndex == 0)
{
if (index != 0)
{
paintString(e, text, left, top, right, bottom, ((index / 3) - 1) * 4 + 1, (index / 3) * 4);
}
}
else if (e.RowIndex == 1)
{
if (index - (3 * (col - 1) / 4) != 0)
{
paintString(e, text, left, top, right, bottom, 2 * (index - (3 * (col - 1) / 4)) - 1, 2 * (index - (3 * (col - 1) / 4)));
}
}
}
}
}
e.Handled = true;
}
}
}
}
重写文字函数
// 该函数用于重新在合并后的单元格中间写入内容,实现文字居中的效果
public void paintString(DataGridViewCellPaintingEventArgs e, string text, int left, int top, int right, int bottom, int Col1, int Col2)
{
SolidBrush fontBrush = new SolidBrush(e.CellStyle.ForeColor);
StringFormat sf = new StringFormat();
sf.Alignment = StringAlignment.Center;
sf.LineAlignment = StringAlignment.Center;
e.Graphics.DrawString("", e.CellStyle.Font, Brushes.Black,
new Rectangle(left, top, right - left, (bottom - top) / 2), sf);
try
{
left = D1.GetColumnDisplayRectangle(Col1, true).Left;
}
catch { }
if (left < 0) left = D1.GetCellDisplayRectangle(-1, -1, true).Width;
try
{
right = D1.GetColumnDisplayRectangle(Col2, true).Right;
}
catch { }
if (right < 0) right = this.Width;
e.Graphics.DrawString(text, e.CellStyle.Font, Brushes.Black,
new Rectangle(left, top + 8, right - left, (bottom - top) / 2), sf);
}