Using winforms in vs2008. I have a DataGridView and I would like to detect when the vertical scroll bar is visible. What event should I register for?
I am adding the
I gave Hans Passant the Check mark since he answered the question asked... However, I went another route for the solution. Since the dialog box is modal, the list of items will not change from the time it is created. So I am able to call the code below to ensure that the textboxes are in the correct location when the dialog first displays.
/// <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;
}
Overriding DGV behavior is usually a huge pain in the neck. Got this going pretty quickly though. Add a new class to your form and paste the code shown below. Compile. Drop the new control from the top of the toolbar onto a form. Implement the ScrollbarVisibleChanged event.
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);
}
}
Instead of using Linq (Adam Butler) you can just iterate through the controls and sign up an event handler that will be called each time the scrollbar visibility changes. I implemented it that way and it works pretty smoothly:
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;
}
do this somewhere after the InitializeComponent() In the handler, do whatever you need to do in response to the visibility change of the vertical scrollbar. Same works for the horizontal scrollbar (replace VScrollBar with HScrollBar):
void c_VisibleChanged(object sender, EventArgs e)
{
VScrollBar vb = sender as VScrollBar;
if (vb.Visible) _DataGridViewScrollbarWidth = vb.Width;
else _DataGridViewScrollbarWidth = 0;
}
If your dgv is inside a panel then you can compare panel and dgv height properties. If dgv's is bigger than panel's then there must be a scrollbar, right?
Like :
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!");
Set the DGV last column's "AutoSizeMode" property to "Fill" and set the TextBox's Width property equal to dgv.Columns["lastcolumn"].Width.
var vScrollbar = dataGridView1.Controls.OfType<VScrollBar>().First();
if (vScrollbar.Visible)
{
}