Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Scroll to bottom of C# DataGridView

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?

like image 462
Motumbo Avatar asked Apr 01 '12 22:04

Motumbo


People also ask

How do I scroll to the bottom of the page?

Of course, you can also click and drag the scroll bar on the side of the page, but that's a slow and imprecise option–especially if you're using a laptop touchpad. No, by far the best way to jump to the top or bottom of a Web page is by tapping your Home or End key, respectively.

How do you scroll to the bottom in CSS?

use css position top keep it at the bottom {position : relative; bottom:0;} .

How do I make Div scroll automatically?

Set the overflow-x:hidden; and overflow-y:auto; that will automatically hide the horizontal scroll bar and present only vertical scrollbar. Here the scroll div will be vertically scrollable.

What is scrollTop in JavaScript?

The Element. scrollTop property gets or sets the number of pixels that an element's content is scrolled vertically. An element's scrollTop value is a measurement of the distance from the element's top to its topmost visible content.


2 Answers

To scroll to bottom of DataGridView try this.

dataGridView1.FirstDisplayedScrollingRowIndex = dataGridView1.RowCount-1; 
like image 188
Mark Hall Avatar answered Sep 29 '22 12:09

Mark Hall


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 
like image 26
Ed Sharp Avatar answered Sep 29 '22 11:09

Ed Sharp