gpt4 book ai didi

c# - 自定义 DataGridView 单元格绘画

转载 作者:行者123 更新时间:2023-11-30 22:06:54 27 4
gpt4 key购买 nike

我正在尝试绘制自己的网格线,因为我想要比默认数据 GridView 线更粗的线。这是我用来执行此操作的代码:

 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));
}
}

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

最佳答案

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

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。顶

您可能需要调整线条的点以考虑这些边界的粗细,此时它们正在细胞外流血。

关于c# - 自定义 DataGridView 单元格绘画,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23275856/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com