tags:

views:

159

answers:

3

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 summing the each cell value in the last column of the grid and displaying that value in a textbox at the bottom of the DataGridView.

I would like this textbox to stay lined up with the cell values (I have made them right aligned since it is $$ values) even after the scroll bar is present.

+2  A: 

Overriding DGV behavior is usually a huge pain in the neck. Got his 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);
    } 
}
Hans Passant
Ouch that seems to be a pain just to detect when a column has been resized due to the scrollbar being shown.
Billy
@Billy: always a mistake to not say why you really need something. Use the DGV's ColumnWidthChanged event.
Hans Passant
A: 

I think there´s no event for that... but you can try with something like this in all the places where the grid can grow:

  • Obtain the number of actual rows in the gridview + the header
  • Multiply that number by the height of each row
  • If the result is greater than the height of the DataGrid, there must be a vertical scrollbar.
Claudia
A: 

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;         
  }
Billy