How to detect the vertical scrollbar in a DataGridView control

后端 未结 7 687
半阙折子戏
半阙折子戏 2021-01-05 05:19

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

相关标签:
7条回答
  • 2021-01-05 05:50

    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;         
      }
    
    0 讨论(0)
  • 2021-01-05 05:54

    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);
        } 
    }
    
    0 讨论(0)
  • 2021-01-05 05:56

    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;
    } 
    
    0 讨论(0)
  • 2021-01-05 05:57

    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!");
    
    0 讨论(0)
  • 2021-01-05 05:57

    Set the DGV last column's "AutoSizeMode" property to "Fill" and set the TextBox's Width property equal to dgv.Columns["lastcolumn"].Width.

    0 讨论(0)
  • 2021-01-05 05:58
    var vScrollbar = dataGridView1.Controls.OfType<VScrollBar>().First();
    if (vScrollbar.Visible)
    {
    }
    
    0 讨论(0)
提交回复
热议问题