datagridview 纵向 横向 合并单元格

时间:2025-04-06 20:36:43

datagridview 单元格合并:纵向以及横向合并参考了****上不知哪位的代码,具体哪位找不到连接了。

纵向合并:

        /// <summary>
/// 纵向合并,即合并数据项的值
/// </summary>
/// <param name="e"></param>
private void DrawCellVer(DataGridViewCellPaintingEventArgs e)
{
if (e.CellStyle.Alignment != DataGridViewContentAlignment.MiddleCenter)
{
e.CellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter;
}
Brush gridBrush = new SolidBrush(this.GridColor);
SolidBrush backBrush = new SolidBrush(e.CellStyle.BackColor);
int cellwidth;
//上面相同的行数
int UpRows = 0;
//下面相同的行数
int DownRows = 0;
//总行数
int count = 0;
if (true)
{
cellwidth = e.CellBounds.Width;
Pen gridLinePen = new Pen(gridBrush);
//获取当前单元格的值,如果为null,就赋值为""
string curValue = e.Value == null ? "" : e.Value.ToString().Trim();
if (!string.IsNullOrEmpty(curValue))
{
#region 获取下面的行数
for (int i = e.RowIndex; i < this.Rows.Count; i++)
{
if (this.Rows[i].Cells[e.ColumnIndex].Value.ToString().Equals(curValue))
{
DownRows++;
if (e.RowIndex != i)
{
cellwidth = cellwidth < this.Rows[i].Cells[e.ColumnIndex].Size.Width ? cellwidth : this.Rows[i].Cells[e.ColumnIndex].Size.Width;
}
}
else
break;
}
#endregion #region 获取上面的行数
for (int i = e.RowIndex; i >= 0; i--)
{
if (this.Rows[i].Cells[e.ColumnIndex].Value.ToString().Equals(curValue))
{
UpRows++;
if (e.RowIndex != i)
{
cellwidth = cellwidth < this.Rows[i].Cells[e.ColumnIndex].Size.Width ? cellwidth : this.Rows[i].Cells[e.ColumnIndex].Size.Width;
}
}
else
break;
}
#endregion
count = DownRows + UpRows - 1; if (count < 2)
{
return;
}
}
else
{
//取下面看是否有为空的单元格,如果为最后一个为空的单元格,则画下边线
if (e.RowIndex < this.Rows.Count - 1)
{
if (this.Rows[e.RowIndex + 1].Cells[e.ColumnIndex].Value != DBNull.Value)
{
if (!string.IsNullOrEmpty(this.Rows[e.RowIndex + 1].Cells[e.ColumnIndex].Value.ToString()))
{
DownRows = 1;
} }
}
}
if (this.Rows[e.RowIndex].Selected)
{
backBrush.Color = e.CellStyle.SelectionBackColor;
//fontBrush.Color = e.CellStyle.SelectionForeColor;
}
//以背景色填充
e.Graphics.FillRectangle(backBrush, e.CellBounds);
//画字符串
PaintingFont(e, cellwidth, UpRows, DownRows, count);
if (DownRows == 1)
{
//画下面的线
e.Graphics.DrawLine(gridLinePen, e.CellBounds.Left, e.CellBounds.Bottom - 1, e.CellBounds.Right - 1, e.CellBounds.Bottom - 1);
count = 0;
}
// 画右边线
e.Graphics.DrawLine(gridLinePen, e.CellBounds.Right - 1, e.CellBounds.Top, e.CellBounds.Right - 1, e.CellBounds.Bottom); e.Handled = true;
}
}

  横向合并:

        /// <summary>
/// 水平合并单元格,即数据头的合并
/// </summary>
/// <param name="e"></param>
private void DrawCellHor(DataGridViewCellPaintingEventArgs e)
{
if (e.CellStyle.Alignment != DataGridViewContentAlignment.MiddleCenter)
{
e.CellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter;
}
Brush gridBrush = new SolidBrush(this.GridColor);
SolidBrush backBrush = new SolidBrush(e.CellStyle.BackColor);
int cellwidth;
//前面相同的行数
int BefRows = 0;
//后面相同的行数
int BehRows = 0;
//总列数
int count = 0;
if (true)
{
cellwidth = e.CellBounds.Width;
Pen gridLinePen = new Pen(gridBrush);
//获取当前单元格的值,如果为null,就赋值为""
string curValue = e.Value == null ? "" : e.Value.ToString().Trim();
if (!string.IsNullOrEmpty(curValue))
{
#region 获取后面的列数
for (int i = e.ColumnIndex; i < this.ColumnCount; i++)
{
if (this.Rows[e.RowIndex].Cells[i].Value.ToString().Equals(curValue))
{
BehRows++;
if (e.ColumnIndex != i)
{
cellwidth = cellwidth < this.Rows[e.RowIndex].Cells[i].Size.Width ? cellwidth : this.Rows[e.RowIndex].Cells[i].Size.Width;
}
}
else
break;
}
#endregion #region 获取前面的列数
for (int i = e.ColumnIndex; i >= 0; i--)
{
if (this.Rows[e.RowIndex].Cells[i].Value.ToString().Equals(curValue))
{
BefRows++;
if (e.ColumnIndex != i)
{
cellwidth = cellwidth < this.Rows[e.RowIndex].Cells[i].Size.Width ? cellwidth : this.Rows[e.RowIndex].Cells[i].Size.Width;
}
}
else
break;
}
#endregion
count = BehRows + BefRows - 1; if (count < 2)
{
return;
}
}
else
{
//取右边看是否有为空的单元格,如果为最后一个为空的单元格,则画右侧线
if (e.ColumnIndex < this.ColumnCount - 1)
{
if (this.Rows[e.RowIndex].Cells[e.ColumnIndex+1].Value != DBNull.Value)
{
if (!string.IsNullOrEmpty(this.Rows[e.RowIndex].Cells[e.ColumnIndex+1].Value.ToString()))
{
BehRows = 1;
} }
}
}
if (this.Rows[e.RowIndex].Selected)
{
backBrush.Color = e.CellStyle.SelectionBackColor;
//fontBrush.Color = e.CellStyle.SelectionForeColor;
}
//以背景色填充
e.Graphics.FillRectangle(backBrush, e.CellBounds);
//画字符串
HorPaintingFont(e, cellwidth, BefRows, BehRows, count);
if (BehRows == 1)
{
//画右边缘的线
e.Graphics.DrawLine(gridLinePen, e.CellBounds.Right - 1, e.CellBounds.Top, e.CellBounds.Right - 1, e.CellBounds.Bottom);
count = 0;
}
// 画下边缘的线
e.Graphics.DrawLine(gridLinePen, e.CellBounds.Left, e.CellBounds.Bottom - 1, e.CellBounds.Right - 1, e.CellBounds.Bottom - 1); e.Handled = true;
}
}

 单元格写值纵向:

        /// <summary>
/// 绘制合并以后的值(纵向)
/// </summary>
/// <param name="e"></param>
/// <param name="cellwidth"></param>
/// <param name="UpRows"></param>
/// <param name="DownRows"></param>
/// <param name="count"></param>
private void PaintingFont(System.Windows.Forms.DataGridViewCellPaintingEventArgs e, int cellwidth, int UpRows, int DownRows, int count)
{
if (e.Value != DBNull.Value)
{
stValue = e.Value.ToString();
}
using (SolidBrush fontBrush = new SolidBrush(e.CellStyle.ForeColor))
{
fontheight = (int)e.Graphics.MeasureString(e.Value.ToString(), e.CellStyle.Font).Height;
fontwidth = (int)e.Graphics.MeasureString(e.Value.ToString(), e.CellStyle.Font).Width;
int cellheight = e.CellBounds.Height; if (e.CellStyle.Alignment == DataGridViewContentAlignment.BottomCenter)
{
e.Graphics.DrawString(stValue, e.CellStyle.Font, fontBrush, e.CellBounds.X + (cellwidth - fontwidth) / 2, e.CellBounds.Y + cellheight * DownRows - fontheight);
}
else if (e.CellStyle.Alignment == DataGridViewContentAlignment.BottomLeft)
{
e.Graphics.DrawString(stValue, e.CellStyle.Font, fontBrush, e.CellBounds.X, e.CellBounds.Y + cellheight * DownRows - fontheight);
}
else if (e.CellStyle.Alignment == DataGridViewContentAlignment.BottomRight)
{
e.Graphics.DrawString(stValue, e.CellStyle.Font, fontBrush, e.CellBounds.X + cellwidth - fontwidth, e.CellBounds.Y + cellheight * DownRows - fontheight);
}
else if (e.CellStyle.Alignment == DataGridViewContentAlignment.MiddleCenter)
{ e.Graphics.DrawString(stValue, e.CellStyle.Font, fontBrush, e.CellBounds.X + (cellwidth - fontwidth) / 2, e.CellBounds.Y - cellheight * (UpRows - 1) + (cellheight * count - fontheight) / 2);
}
else if (e.CellStyle.Alignment == DataGridViewContentAlignment.MiddleLeft)
{
e.Graphics.DrawString(stValue, e.CellStyle.Font, fontBrush, e.CellBounds.X, e.CellBounds.Y - cellheight * (UpRows - 1) + (cellheight * count - fontheight) / 2);
}
else if (e.CellStyle.Alignment == DataGridViewContentAlignment.MiddleRight)
{
e.Graphics.DrawString(stValue, e.CellStyle.Font, fontBrush, e.CellBounds.X + cellwidth - fontwidth, e.CellBounds.Y - cellheight * (UpRows - 1) + (cellheight * count - fontheight) / 2);
}
else if (e.CellStyle.Alignment == DataGridViewContentAlignment.TopCenter)
{
e.Graphics.DrawString(stValue, e.CellStyle.Font, fontBrush, e.CellBounds.X + (cellwidth - fontwidth) / 2, e.CellBounds.Y - cellheight * (UpRows - 1));
}
else if (e.CellStyle.Alignment == DataGridViewContentAlignment.TopLeft)
{
e.Graphics.DrawString(stValue, e.CellStyle.Font, fontBrush, e.CellBounds.X, e.CellBounds.Y - cellheight * (UpRows - 1));
}
else if (e.CellStyle.Alignment == DataGridViewContentAlignment.TopRight)
{
e.Graphics.DrawString(stValue, e.CellStyle.Font, fontBrush, e.CellBounds.X + cellwidth - fontwidth, e.CellBounds.Y - cellheight * (UpRows - 1));
}
else
{
e.Graphics.DrawString(stValue, e.CellStyle.Font, fontBrush, e.CellBounds.X + (cellwidth - fontwidth) / 2, e.CellBounds.Y - cellheight * (UpRows - 1) + (cellheight * count - fontheight) / 2);
}
}
}

  单元格写值横向:

        /// <summary>
/// 水平方向写值
/// </summary>
/// <param name="e"></param>
/// <param name="cellwidth"></param>
/// <param name="UpRows"></param>
/// <param name="DownRows"></param>
/// <param name="count"></param>
private void HorPaintingFont(System.Windows.Forms.DataGridViewCellPaintingEventArgs e, int cellwidth, int UpRows, int DownRows, int count)
{
using (SolidBrush fontBrush = new SolidBrush(e.CellStyle.ForeColor))
{
fontheight = (int)e.Graphics.MeasureString(e.Value.ToString(), e.CellStyle.Font).Height;
fontwidth = (int)e.Graphics.MeasureString(e.Value.ToString(), e.CellStyle.Font).Width;
int cellHeight = e.CellBounds.Height; if (e.Value != DBNull.Value)
{
stValue = e.Value.ToString();
}
if (e.CellStyle.Alignment == DataGridViewContentAlignment.MiddleCenter)
{
e.Graphics.DrawString(stValue, e.CellStyle.Font, fontBrush, e.CellBounds.X - cellwidth * (UpRows - 1) + (cellwidth * count - fontwidth) / 2, e.CellBounds.Y + (cellHeight - fontheight) / 2);
}
}
}

  在次备忘一下。