在vs2008中使用winforms。我有一个DataGridView,我想检测垂直滚动条何时可见。我应该注册什么事件?
我在网格的最后一列中添加每个单元格值的求和,并在DataGridView底部的文本框中显示该值。
我希望这个文本框与单元格值保持一致(即使在滚动条存在之后,我已将它们对齐,因为它是$$值)。
答案 0 :(得分:22)
var vScrollbar = dataGridView1.Controls.OfType<VScrollBar>().First();
if (vScrollbar.Visible)
{
}
答案 1 :(得分:8)
超越DGV行为通常是颈部的巨大痛苦。尽管如此,这很快就会发生。在表单中添加一个新类并粘贴下面显示的代码。编译。将新控件从工具栏顶部拖放到表单上。实现ScrollbarVisibleChanged事件。
using System;
using System.Windows.Forms;
class MyDgv : DataGridView {
public event EventHandler ScrollbarVisibleChanged;
public MyDgv() {
this.VerticalScrollBar.VisibleChanged += new EventHandler(VerticalScrollBar_VisibleChanged);
}
public bool VerticalScrollbarVisible {
get { return VerticalScrollBar.Visible; }
}
private void VerticalScrollBar_VisibleChanged(object sender, EventArgs e) {
EventHandler handler = ScrollbarVisibleChanged;
if (handler != null) handler(this, e);
}
}
答案 2 :(得分:1)
除了使用Linq(Adam Butler)之外,您还可以遍历控件并注册一个事件处理程序,每次滚动条可见性更改时都会调用该事件处理程序。我以这种方式实现它并且它运行得非常顺利:
private System.Windows.Forms.DataGridView dgCounterValues;
private Int32 _DataGridViewScrollbarWidth;
// get vertical scrollbar visibility handler
foreach (Control c in dgCounterValues.Controls)
if (c.GetType().ToString().Contains("VScrollBar"))
{
c.VisibleChanged += c_VisibleChanged;
}
在InitializeComponent()之后的某处执行此操作 在处理程序中,为响应垂直滚动条的可见性更改,执行您需要执行的操作。水平滚动条的作用相同(用HScrollBar替换VScrollBar):
void c_VisibleChanged(object sender, EventArgs e)
{
VScrollBar vb = sender as VScrollBar;
if (vb.Visible) _DataGridViewScrollbarWidth = vb.Width;
else _DataGridViewScrollbarWidth = 0;
}
答案 3 :(得分:0)
我认为没有任何事件......但你可以在网格可以增长的所有地方尝试这样的事情:
答案 4 :(得分:0)
自从他回答了问题后,我给了Hans Passant一个Check标记......然而,我又采取了另一条路线来解决问题。由于对话框是模态的,因此项目列表不会从创建时间更改。因此,我可以调用下面的代码,以确保在对话框首次显示时文本框位于正确的位置。
/// <summary>
/// Horizontally shifts the label and text boxes that display the total
/// values so that the totals remain aligned with the columns.
/// </summary>
private void ShiftTotalsDisplay(DataGridView grid, Label firstLabel,
TextBox secondTextBox, TextBox thirdTextBox)
{
//Note if you have a rowheader add the width here also.
int nameRightLoc = grid.Location.X +
grid.Columns[0].Width;
int fpRightLoc = nameRightLoc +
grid.Columns[0].DividerWidth +
grid.Columns[1].Width;
int dlRightLoc = fpRightLoc +
grid.Columns[1].DividerWidth +
grid.Columns[2].Width;
Point loc = firstLabel.Location;
loc.X = nameRightLoc - firstLabel.Width - 2;
firstLabel.Location = loc;
loc = secondTextBox.Location;
loc.X = fpRightLoc - secondTextBox.Width - 2;
secondTextBox.Location = loc;
loc = thirdTextBox.Location;
loc.X = dlRightLoc - thirdTextBox.Width - 2;
thirdTextBox.Location = loc;
}
答案 5 :(得分:0)
如果你的dgv在面板内,那么你可以比较面板和dgv高度属性。如果dgv比面板大,那么必须有一个滚动条,对吗?
喜欢:
int panel_height = pnl.Height;
int dgv_height = (dgv.RowCount + 1) * 24; // You can play around with this 24 according to your cell styles
if (dgv_height > panel_height) MessageBox.Show("Voila!");
答案 6 :(得分:0)
设置DGV最后一栏&#39;&#34; AutoSizeMode&#34;财产到&#34;填写&#34;并将TextBox的Width属性设置为等于dgv.Columns [&#34; lastcolumn&#34;]。宽度。