如何在DataGridView中的行之间绘制垂直线?

时间:2019-06-07 08:08:17

标签: vb.net datagridview .net-4.5 paint

我需要在行之间绘制某种括号,以使用户知道行具有某种关系(我不想合并单元格)。到目前为止看起来不错,但是滚动时会变得一团糟。

When loading When scrolling down and up

我无法弄清楚。是否没有办法画出持久的垂直线以使它们保持秩序?

Private Sub dgcontrol_CellPainting(ByVal sender As Object, ByVal e As DataGridViewCellPaintingEventArgs) Handles dgcontrol.CellPainting
If sender.Columns(e.ColumnIndex).Name = "xrow2" Then
 Dim rec1 As Rectangle = dgcontrol.GetCellDisplayRectangle(2, 0, False)
 Dim rec2 As Rectangle = dgcontrol.GetCellDisplayRectangle(2, 1, False)
 Dim gridBrush As Brush = New SolidBrush(Color.Red)
 Dim gridLinePen As Pen = New Pen(gridBrush, 2)
 e.Graphics.DrawLine(gridLinePen, New Point(rec1.X + rec1.Width - 2 - rec1.Width / 5, rec1.Y + rec1.Height / 3), New Point(rec1.X + rec1.Width - 2, rec1.Y + rec1.Height / 3))
 e.Graphics.DrawLine(gridLinePen, New Point(rec2.X + rec1.Width - 2 - rec1.Width / 5, rec2.Y + rec2.Height - rec2.Height / 3), New Point(rec2.X + rec1.Width - 2, rec2.Y + rec2.Height - rec2.Height / 3))
 e.Graphics.DrawLine(gridLinePen, New Point(rec1.X + rec1.Width - 3, rec1.Y + rec1.Height / 3), New Point(rec2.X + rec1.Width - 3, rec2.Y + rec2.Height - rec2.Height / 3))
 e.Handled = False
End If End Sub

0 个答案:

没有答案