Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 630
  • Last Modified:

Vertical line in datagridview

I got the horizontal line working and tried the same technique to draw a different color vertical line between two columns but it doesn't work.

Try
            Dim g As Graphics = DataGridView1.CreateGraphics()
            Dim p As Pen = New Pen(Color.Black)
            Dim rect1 As Rectangle, rect2 As Rectangle
            rect1 = DataGridView1.GetCellDisplayRectangle(16, 0, True)
            rect2 = DataGridView1.GetCellDisplayRectangle(16, DataGridView1.Rows.Count - 1, True)
            Dim linefrom As Point, lineto As Point
            linefrom.X = rect1.Left
            linefrom.Y = rect1.Top + 1
            lineto.X = rect2.Left
            lineto.Y = rect2.Bottom + 1
            g.DrawLine(p, linefrom, lineto)
        Catch ex As Exception
            errlog.WriteError("Error in paint lines: " & ex.Message, "PaintLines")
        End Try
0
ddepuemd
Asked:
ddepuemd
  • 2
  • 2
1 Solution
 
Bob LearnedCommented:
It might be easier if you handle the CellPainting event, and check to see if the cell's ColumnIndex is the one that you need, and then you give the illusion that you are drawing a single continuous line...
0
 
ddepuemdAuthor Commented:
I'm not sure I understand what you mean.  How could I do that?
0
 
Bob LearnedCommented:
Example:


    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
 
        Me.dgv.RowTemplate.Height = Me.dgv.ClientSize.Height / 3
 
        Me.dgv.RowCount = 3
        Me.dgv.ColumnCount = 3
        Me.dgv.RowHeadersVisible = False
        Me.dgv.ColumnHeadersVisible = False
 
        Me.dgv.Columns(0).Width = Me.dgv.ClientSize.Width / 3
        Me.dgv.Columns(1).Width = Me.dgv.Columns(0).Width
        Me.dgv.Columns(2).Width = Me.dgv.Columns(0).Width
 
        Me.dgv.ScrollBars = ScrollBars.None
        Me.dgv.DefaultCellStyle.SelectionBackColor = Me.dgv.DefaultCellStyle.BackColor
        Me.dgv.DefaultCellStyle.SelectionForeColor = Me.dgv.DefaultCellStyle.ForeColor
 
        AddHandler Me.dgv.CellPainting, AddressOf OnCellPainting
 
    End Sub
 
    Private Sub OnCellPainting(ByVal sender As Object, ByVal e As DataGridViewCellPaintingEventArgs)
 
        If e.ColumnIndex = 1 Then
            Dim x As Integer = e.CellBounds.Right - 3
            Dim y1 As Integer = e.CellBounds.Top
            Dim y2 As Integer = e.CellBounds.Bottom
 
            e.PaintBackground(e.ClipBounds, e.State = DataGridViewElementStates.Selected)
            e.PaintContent(e.ClipBounds)
            e.Graphics.DrawLine(Pens.Red, x, y1, x, y2)
 
            e.Handled = True
 
        End If
 
    End Sub

Open in new window

DataGridView----Vertical-Line.png
0
 
ddepuemdAuthor Commented:
thank you
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now