繁体   English   中英

检查滚动条在数据网格视图中是否可见

[英]check if a scroll bar is visible in a datagridview

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

你可以试试这个:

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

我更喜欢这个:

//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]);

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

if (DataGridView.Controls.OfType<VScrollBar>().First().Visible)
{
    //want you want to do
}

可以使用ScrollBars枚举来质疑DataGridViewScrollbars属性,方法是用您感兴趣的一个屏蔽它,如下所示:

if ((dataGridView1.ScrollBars & ScrollBars.Vertical) != ScrollBars.None) ...

请注意,这里的两个“滚动条”是不同的!

要确定是否存在垂直滚动条,您需要检查可见行的高度并与 datagridview 高度进行比较。

if(dgv1.Height > dgv1.Rows.GetRowsHeight(DataGridViewElementStates.Visible))
{
    // Scrollbar not visible
}
else
{
    // Scrollbar visible
}

虽然更准确地说,您可能需要检查列宽,因为水平滚动条的存在可能会创建一个垂直滚动条,否则就不存在。

terrybozzio 的答案仅在您使用System.Linq命名空间时才有效。 不使用System.Linq的解决方案System.Linq所示:

foreach (var Control in dataGridView1.Controls)
{
    if (Control.GetType() == typeof(VScrollBar))
    {
        //your checking here
        //specifically... if (((VScrollBar)Control).Visible)
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM