41

I'm trying to scroll to bottom of a DataGridView in a C# WinForm.

This code works with a TextBox:

textbox_txt.SelectionStart = textbox_txt.Text.Length;
textbox_txt.ScrollToCaret();

... but I don't know how to do it with a DataGridView. Any help, please?

1

2 Answers 2

96

To scroll to bottom of DataGridView try this.

dataGridView1.FirstDisplayedScrollingRowIndex = dataGridView1.RowCount-1;
3
  • 3
    Useful, thanks. If your DataGridView has hidden rows then you need to check row visibility because the DataGridView does not let you scroll to invisible rows.
    – 2.718
    Jun 26, 2015 at 3:09
  • 1
    Good one! If your DataGridView is bound to a BindingSource, you can achieve the same result with dataGridView1.FirstDisplayedScrollingRowIndex = BindingSource.Count-1; Aug 6, 2015 at 20:44
  • 2
    This doesn't work if there's a currently selected cell because the DataGridView tries to keep that cell visible. Is there any way to force the scroll anyway? I don't want to change the current cell, just let it scroll off the screen.
    – dwilliss
    Feb 13, 2017 at 21:55
6

As a commercial programmer, I use a C# DLL to handle all my DataGridView projects which gives me language freedom for whatever project I undertake. All of my programs trap all key-presses so that I can use them for my own purposes. For DataGridView scrolling, I use the PageUp/PageDown keys for a single page, Ctrl/Page for single line and Alt/Page for top (Up) and bottom (Down). C# code and Basic calling sequence as follows:

//---------- C# Dll Partial Source -----------

public int RowShow
   { get { return vu.DisplayedRowCount(false); } }

public int RowCount 
   { get { return vu.RowCount; } }

public void PageMove(int rows)
{
    int rowlimit = vu.RowCount - 1;
    int calc = vu.FirstDisplayedScrollingRowIndex + rows;

    if (calc > rowlimit) calc = rowlimit;  // Go to bottom
    if (calc < 0)        calc = 0;         // Go to top

    vu.FirstDisplayedScrollingRowIndex = calc;
}

// ---------- End Data Grid View ----------



//---------- Calling Program C# ----------

public void Page_Key(int val, int lastKey)
{
    int inc = 1;                // vu is DataGridView

    If (val == 33) inc = -1;

    int rowsDisp = vu.RowShow;  // # of rows displayed
    int rowsMax  = vu.RowCount; // # of rows in view
    int rows     = 0;

    switch (lastKey)
    {        
      case 17:                  // Ctrl prior to Page
        rows = inc;
        break; 
      case 19:                  // Alt prior to Page
        rows = rowsMax * inc;
        break;
      default:
        rows = rowsDisp * inc
        break;
    }  // end switch

  vu.PageMove(rows)
} // end Page_Key



'----- Calling Program B4PPC, VB -----

Sub Page_Key(val,lastKey)     ' 33=PageUp, 34=Down
    inc = 1                   ' vu is DataGridView

    If val = 33 then inc = -1

    rowsDisp = vu.RowShow     ' # of rows displayed
    rowsMax  = vu.RowCount    ' # of rows in view
    rows     = 0

    Select lastKey
      Case 17                 ' Ctrl prior to Page
        rows = inc 
      Case 19                 ' Alt prior to Page
        rows = rowsMax * inc
      Case Else
        rows = rowsDisp * inc
    End Select

    lastKey = ""

    vu.PageMove(rows)
End Sub

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.