自定义DataGridView单元格绘画 [英] Custom DataGridView Cell Painting

查看:157
本文介绍了自定义DataGridView单元格绘画的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图绘制自己的网格线,因为我想要比默认的数据网格视图行更粗的线条。这是我使用的代码:

I'm trying to draw my own grid lines because I want thicker lines than the default data grid view lines. This is the code I'm using to do it:

 private void dgv_Wafer_CellPainting(object sender, DataGridViewCellPaintingEventArgs e)
    {
        using (Pen p = new Pen(Brushes.Black, 12))
        {
            e.Graphics.DrawLine(p, new Point(0, e.CellBounds.Bottom), new Point(e.CellBounds.Right, e.CellBounds.Bottom));
        }
        using (Pen p = new Pen(Brushes.Black, 6))
        {
            e.Graphics.DrawLine(p, new Point(e.CellBounds.Right, 0), new Point(e.CellBounds.Right - 1, e.CellBounds.Bottom));
        }
    }

线条被绘制,但是水平线条不会在最后一列中绘制,垂直线将不会在最后一行中绘制。这些线条正在创建一个列和行太小的网格。有没有人知道如何解决这个问题?

The lines are drawn but the horizontal lines won't be drawn in the last column and the vertical lines won't be drawn in the last row. The lines are creating a grid that is a column and row too small. Does anyone know how to fix this?

推荐答案

尝试设置 e.Handled = true; code>来控制画。添加到单元格的默认绘图中:

Try setting the e.Handled = true; to control the painting. Add back in the default painting of the cells:

e.PaintBackground(e.ClipBounds, true);
e.PaintContent(e.ClipBounds);
using (Pen p = new Pen(Brushes.Black, 12)) {
  e.Graphics.DrawLine(p, new Point(e.CellBounds.Left, e.CellBounds.Bottom),
                         new Point(e.CellBounds.Right, e.CellBounds.Bottom));
}
using (Pen p = new Pen(Brushes.Black, 6)) {
  e.Graphics.DrawLine(p, new Point(e.CellBounds.Right, e.CellBounds.Top),
                         new Point(e.CellBounds.Right, e.CellBounds.Bottom));
}
e.Handled = true;

您的代码也在左侧和上方使用0,但CellBounds值基于控件的内部空格,所以你应该使用 e.CellBounds.Left e.CellBounds.Top

Your code was also using 0 for left and top, but the CellBounds values are based on the control's interior space, so you should use e.CellBounds.Left and e.CellBounds.Top

您可能想要调整线的点数以考虑这些边框的厚度,它们目前正在单元格外部流出。

You might want to adjust the points of your line to account for the thickness of those borders, they are bleeding outside of the cell at the moment.

这篇关于自定义DataGridView单元格绘画的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆