简体   繁体   English

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

[英]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.我无法使用事件,因为我的代码已经在事件中。

you can try this out:你可以试试这个:

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

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
}

The DataGridView 's Scrollbars Property can be questioned using the ScrollBars Enumeration by masking it with the one you are interested in like this:可以使用ScrollBars枚举来质疑DataGridViewScrollbars属性,方法是用您感兴趣的一个屏蔽它,如下所示:

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

Note, that the two 'ScrollBars' are different things here!请注意,这里的两个“滚动条”是不同的!

To determine if the vertical scrollbar is present, you need to check how tall your visible rows are and compare against the datagridview height.要确定是否存在垂直滚动条,您需要检查可见行的高度并与 datagridview 高度进行比较。

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

Though to be more exact you may need to include a check of column widths as the presence of a horizontal scrollbar could create a vertical scrollbar that otherwise isn't there.虽然更准确地说,您可能需要检查列宽,因为水平滚动条的存在可能会创建一个垂直滚动条,否则就不存在。

The answer from terrybozzio works only if you use the System.Linq namespace. terrybozzio 的答案仅在您使用System.Linq命名空间时才有效。 A solution without using System.Linq is shown below:不使用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