Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

check if a scroll bar is visible in a datagridview

I would like to display something if the data grid View is long and showing a scroll bar but don't know how to check if the scroll bar is visible. I can't simply add the rows since some may be not visible. I can't use an event since my code is already in an event.

like image 897
ZNackasha Avatar asked Jun 30 '14 21:06

ZNackasha


2 Answers

I prefer this one :

//modif is a modifier for the adjustment of the Client size of the DGV window
//getDGVWidth() is a custom method to get needed width of the DataGridView

int modif = 0;
if (DataGridView.Controls.OfType<VScrollBar>().First().Visible)
{
    modif = SystemInformation.VerticalScrollBarWidth;
}
this.ClientSize = new Size(getDGVWidth() + modif, [wantedSizeOfWindow]);

so the only Boolean condition you need is:

if (DataGridView.Controls.OfType<VScrollBar>().First().Visible)
{
    //want you want to do
}
like image 92
Sirmyself Avatar answered Sep 19 '22 05:09

Sirmyself


The answer from terrybozzio works only if you use the System.Linq namespace. A solution without using System.Linq is shown below:

foreach (var Control in dataGridView1.Controls)
{
    if (Control.GetType() == typeof(VScrollBar))
    {
        //your checking here
        //specifically... if (((VScrollBar)Control).Visible)
    }
}
like image 27
G. Maniatis Avatar answered Sep 20 '22 05:09

G. Maniatis