对dataGridView指定列相同单元格合并
Posted zhujie-com
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了对dataGridView指定列相同单元格合并相关的知识,希望对你有一定的参考价值。
//写在dataGridView1控件的CellPainting事件中
if (e.ColumnIndex==0 && e.RowIndex!=-1 || e.ColumnIndex==1 && e.RowIndex!=-1)//对第1列和第2列相同单元格进行合并
{
Brush datagridBrush=new SolidBrush(dataGridView1.GridColor);
SolidBrush grouplinebBrush =new SolidBrush(e.CellStyle.BackColor);
using (Pen datagridlinePen=new Pen(datagridBrush))
{
e.Graphics.FillRectangle(grouplinebBrush,e.CellBounds);
if (e.RowIndex<dataGridView1.RowCount-1 && dataGridView1.Rows[e.RowIndex+1].Cells[e.ColumnIndex].Value !=null && dataGridView1.Rows[e.RowIndex+1].Cells[e.ColumnIndex].Value.ToString()!=e.Value.ToString())
{
e.Graphics.DrawLine(datagridlinePen,e.CellBounds.Left,e.CellBounds.Bottom-1,e.CellBounds.Right,e.CellBounds.Bottom-1);
e.Graphics.DrawLine(datagridlinePen,e.CellBounds.Right-1,e.CellBounds.Top,e.CellBounds.Right-1,e.CellBounds.Bottom);
}
else
{
e.Graphics.DrawLine(datagridlinePen,e.CellBounds.Right-1,e.CellBounds.Top,e.CellBounds.Right-1,e.CellBounds.Bottom);
}
if (e.RowIndex==dataGridView1.Rows.Count-1)
{
e.Graphics.DrawLine(datagridlinePen,e.CellBounds.Left,e.CellBounds.Bottom-1,e.CellBounds.Right,e.CellBounds.Bottom-1);
}
if (e.Value!=null)
{
if (!(e.RowIndex>0 && dataGridView1.Rows[e.RowIndex-1].Cells[e.ColumnIndex].Value.ToString()==e.Value.ToString()))
{
e.Graphics.DrawString(e.Value.ToString(),e.CellStyle.Font,Brushes.Black,e.CellBounds.X+2,e.CellBounds.Y+5,StringFormat.GenericDefault);
}
}
e.Handled = true;
}
}
以上是关于对dataGridView指定列相同单元格合并的主要内容,如果未能解决你的问题,请参考以下文章
哪位高手帮帮忙,winform datagridview单元格合并啊,谢谢了,解决问题了再加赏啊,谢谢
C#Winform中DataGridView合并单元格的问题?