Detect if vertical scroll bar is present on datagridview

lonewolf32

Newcomer
Joined
May 3, 2006
Messages
17
I figured out how to determine the width of the vertical scroll bar in the datagrid view (using SystemInformation namespace) but I found no clue as to how to determine if it is displayed or not (i.e. do I have enough rows in my dgv to cause it to be displayed). I am computing column widths during the resize event of the dgv but the fact that the vertical scroll bar is present or not present comes into play.

Does any one know how to tell if the vertical scroll bar is displayed in a datagridview?

Thanks.
 
Hi Lonewolf,


I don't know of any 'simple' way of doing it, but you could use something like this (it does need tidying up, though)...
Visual Basic:
With dgvInfo
    'The 2 adds a small arbetrary amount on to get past the borders...
    Dim topAdd As Int32 = 2
    'Check to see if a header row is displayed...
    If .RowHeadersVisible Then topAdd += .Rows(0).Height
    'Get a top and bottom point row object index..
    Dim top As DataGridView.HitTestInfo = .HitTest(2, topAdd)
    Dim bot As DataGridView.HitTestInfo = .HitTest(.Right - 2, .Bottom - 2)
    'Check to see if the number of rows displayed is less than the number of rows in the list...
    Dim sbVis As Boolean = top.RowIndex - bot.RowIndex < .RowCount
End With

There's probably a better way, as this isn't really the cleanest method. Also, I've forgotten to take the horizontal scrollbar into account - you'll need to code for this too.


Paul.
 
Just check datagridview.PreferredSize.Height property. If it is greater than datagridview.Height, then the scrollbar is visible.
 
Yet another way is:

detect if dataGridView.Controls[1].Visible = true

Control 0 = Horizontal Scroll Bar
Control 1 = Vertical Scroll Bar

You can also get the width with this if you want something more precise. Cast as a scroll bar and the possibilities are endless.
 
Back
Top