首页 > Windows开发 > 详细

对dataGridView指定列相同单元格合并

时间:2019-12-19 10:29:18      阅读:133      评论:0      收藏:0      [点我收藏+]

//写在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指定列相同单元格合并

原文:https://www.cnblogs.com/zhujie-com/p/12065689.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!