如果数据网格视图很长并且显示滚动条,我希望显示一些内容,但是不知道如何检查滚动条是否可见。我不能简单地添加行,因为有些行可能不可见。我不能使用事件,因为我的代码已经在事件中。
如果数据网格视图很长并且显示滚动条,我希望显示一些内容,但是不知道如何检查滚动条是否可见。我不能简单地添加行,因为有些行可能不可见。我不能使用事件,因为我的代码已经在事件中。
你可以尝试这个:
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
}
var vScrollBar = dgvEntity.Controls.OfType<VScrollBar>().FirstOrDefault(); if (vScrollBar != null && vScrollBar.Visible) vScrollbarWidth = SystemInformation.VerticalScrollBarWidth;
- jacktricDataGridView
的 Scrollbars
属性可以使用 ScrollBars
枚举来查询,通过与您感兴趣的枚举进行屏蔽即可。操作方法如下:
if ((dataGridView1.ScrollBars & ScrollBars.Vertical) != ScrollBars.None) ...
terrybozzio的答案只对使用System.Linq
命名空间的情况有效。
以下是一种不使用System.Linq
的解决方案:
foreach (var Control in dataGridView1.Controls)
{
if (Control.GetType() == typeof(VScrollBar))
{
//your checking here
//specifically... if (((VScrollBar)Control).Visible)
}
}
要确定垂直滚动条是否存在,您需要检查可见行的高度并将其与datagridview的高度进行比较。
if(dgv1.Height > dgv1.Rows.GetRowsHeight(DataGridViewElementStates.Visible))
{
// Scrollbar not visible
}
else
{
// Scrollbar visible
}
更准确地说,您可能需要检查列宽度,因为水平滚动条的存在可能会创建一个本来不存在的垂直滚动条。