当前位置:编程学习 > C#/ASP.NET >>

DataGridView 合并单元格 重绘 显示混乱

求救

datagridview合并单元格,我是用的重绘,自定义类DGV继承DataGridView控件,
  重写
      protected override void OnCellPainting(DataGridViewCellPaintingEventArgs e)
  来实现

原始单元格是这样的,三行三列


合并首列相同单元格,完全也没有问题


想同理合并第二列内以及第三列内相同单元格却出现了混乱现象


求大神指点
原代码如下:

        protected override void OnCellPainting(DataGridViewCellPaintingEventArgs e)
        {
          if (e.ColumnIndex == 0)
          {

                using
                    (
                    Brush gridBrush = new SolidBrush(this.GridColor),
                    backColorBrush = new SolidBrush(e.CellStyle.BackColor)
                    )
                {
                    using (Pen gridLinePen = new Pen(gridBrush))
                    {
                        // 清除单元格
                        e.Graphics.FillRectangle(backColorBrush, e.CellBounds);

                        // 画 Grid 边线(仅画单元格的底边线和右边线)
                        //   如果下一行和当前行的数据不同,则在当前的单元格画一条底边线
                        if (e.RowIndex < this.Rows.Count - 1 &&
                        this.Rows[e.RowIndex + 1].Cells[e.ColumnIndex].Value.ToString() !=
                        e.Value.ToString())
                        {
                            e.Graphics.DrawLine(gridLinePen, e.CellBounds.Left,
                            e.CellBounds.Bottom - 1, e.CellBounds.Right - 1,
                            e.CellBounds.Bottom - 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.RowIndex > 0 &&
                            this.Rows[e.RowIndex - 1].Cells[e.ColumnIndex].Value.ToString() ==
                            e.Value.ToString())
                            { }
                            else
                            {
                                e.Graphics.DrawString((String)e.Value, e.CellStyle.Font,
                                    Brushes.Black, e.CellBounds.X + 2,
                                    e.CellBounds.Y + 5, StringFormat.GenericDefault);
                            }
                        }
                   e.Handled = true;
                    }
                }
         }
}
--------------------编程问答-------------------- 肯定是重绘有问题 --------------------编程问答-------------------- 有没有大神来帮我啊? --------------------编程问答-------------------- --------------------编程问答-------------------- --------------------编程问答-------------------- 为什么管理员要删掉别人对本贴的回复啊
补充:.NET技术 ,  C#
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,