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

查看:47
本文介绍了自定义 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; 来控制绘画.添加回单元格的默认绘制:

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.Lefte.CellBounds.置顶

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天全站免登陆