Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Hide/Disable DataGridView Column/Row Resizing Line

Does anybody know a way of disabling the line that appears when resizing datagridview rows and columns. This line flickers a lot, so I'd rather draw my own solid line myself and disable the default one.

enter image description here

I was hoping by drawing my own thick line (which I've done) it would draw over the top of the default flickering one, but unfortunately both lines then appear, the flickering one usually appears slightly to the right or left of my solid one. I don't think it's relevant, but code for drawing the line below.

Private Sub DataGridView1_Paint(sender As Object, e As PaintEventArgs) Handles DataGridView1.Paint

    If resizingColumns = True Then

        Dim penRed As Pen
        penRed = New Pen(color.Red, 3)

        Dim cursorPosition As Integer = Me.DataGridView1.PointToClient(New Point(Cursor.Position.X, Cursor.Position.Y)).X

        e.Graphics.DrawLine(penRed, cursorPosition, 0, cursorPosition, Me.DataGridView1.Size.Height)

    End If

End Sub

The only other alternative that I can think of (which I don't really want to do) is set AllowUserToResizeColumns to false (which would also hide the column resizing line) and then using the mouse events to resize the columns programmatically.

Any help or direction would be greatly appreciated.

like image 508
Jarron Avatar asked Oct 27 '22 22:10

Jarron


1 Answers

I noticed that if you create a derived DataGridView and enable its DoubleBuffered property that the resizing indicator line does not appear. Using that information, I created the following proof-of-concept control that can be used in place of the base DataGridView control.

Public Class MyDGV : Inherits DataGridView
  Private resizePen As New Pen(Color.Red, 3)

  Public Sub New()
    MyBase.New
    DoubleBuffered = True
  End Sub

  Protected Overrides Sub Dispose(disposing As Boolean)
    MyBase.Dispose(disposing)
    If resizePen IsNot Nothing Then
      resizePen.Dispose()
      resizePen = Nothing
    End If
  End Sub

  Private ReadOnly Property InColumnResize As Boolean
    Get
      Return (MouseButtons = MouseButtons.Left) AndAlso (Cursor = Cursors.SizeWE)
    End Get
  End Property

  Protected Overrides Sub OnMouseMove(e As MouseEventArgs)
    MyBase.OnMouseMove(e)
    If InColumnResize Then Invalidate()
  End Sub

  Protected Overrides Sub OnPaint(e As PaintEventArgs)
    MyBase.OnPaint(e)
    If InColumnResize Then
      Dim cursorPosition As Integer = Me.PointToClient(New Point(Cursor.Position.X, Cursor.Position.Y)).X
      e.Graphics.DrawLine(resizePen, cursorPosition, 0, cursorPosition, Me.Size.Height)
    End If
  End Sub

End Class

I needed to Invalidate the control during a resizing to remove the previously draw line. Perhaps just invalidating the area where the previous line would be better.

The property InColumnResize is admittedly a complete hack job; perhaps the logic you use to set resizingColumns is better.

like image 78
TnTinMn Avatar answered Dec 02 '22 14:12

TnTinMn