Custom Datagrid View Problems
Hi Everyone,
I have a small problem that I can't work out how to fix.
My application requires a data-grid to organise values on-screen. I want a scroll bar to be visible on the right-hand side at all times. If there are not enough items to require scrolling it should no longer be enabled.
I found a great article explaining how to do this and the code is here ...
Code:
Public Class CustomDataGridView
Inherits DataGridView
Public Sub New()
'make scrollbar visible & hook up handler
VerticalScrollBar.Visible = True
AddHandler VerticalScrollBar.VisibleChanged, AddressOf ShowScrollBars
End Sub 'New
Private CAPTIONHEIGHT As Integer = 21
Private BORDERWIDTH As Integer = 2
Private Sub ShowScrollBars(ByVal sender As Object, ByVal e As EventArgs)
If Not VerticalScrollBar.Visible Then
Dim width As Integer = VerticalScrollBar.Width
VerticalScrollBar.Location = New Point((Me.ClientRectangle.Width - width - BORDERWIDTH + 1), CAPTIONHEIGHT - 20)
VerticalScrollBar.Size = New Size(width, (Me.ClientRectangle.Height + 20) - CAPTIONHEIGHT - BORDERWIDTH)
VerticalScrollBar.Show()
End If
End Sub
End Class
The problem is that when I disable and then re-enable this custom datagrid programmatically, the custom scroll bar that is always visible then shows a strange value, minimum and maximum values and then clicking on it causes the program to crash with ...
System.ArgumentOutOfRangeException: Value of '-43' is not valid for 'Value'. 'Value' should be between 'minimum' and 'maximum'.
Parameter name: Value
at System.Windows.Forms.ScrollBar.set_Value(Int32 value)
at System.Windows.Forms.DataGridView.set_VerticalOffset(Int32 value)
Why is this happening? - And what can I change to make sure that after re-enabling the control the correct values are shown?
Many Thanks.
DJ PIP