检查数据网格视图中是否显示滚动条

Gil*_*h22 10 .net c# datagridview winforms

如果数据网格视图很长并且显示滚动条但我不知道如何检查滚动条是否可见,我想显示一些内容.我不能简单地添加行,因为有些行可能不可见.我不能使用事件,因为我的代码已经在事件中.

ter*_*zio 12

你可以尝试一下:

foreach (var scroll in dataGridView1.Controls.OfType<VScrollBar>())
{
   //your checking here
   //specifically... if(scroll.Visible)
}
Run Code Online (Sandbox Code Playgroud)


Sir*_*elf 5

我更喜欢这个:

//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]);
Run Code Online (Sandbox Code Playgroud)

所以你需要的唯一布尔条件是:

if (DataGridView.Controls.OfType<VScrollBar>().First().Visible)
{
    //want you want to do
}
Run Code Online (Sandbox Code Playgroud)