滚动到 C# DataGridView 的底部

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9969061/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-09 11:25:42  来源:igfitidea点击:

Scroll to bottom of C# DataGridView

c#winformsdatagridviewscrollbar

提问by Motumbo

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

我正在尝试在 C# WinForm 中滚动到 DataGridView 的底部。

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?

...但我不知道如何用 DataGridView 做到这一点。请问有什么帮助吗?

采纳答案by Mark Hall

To scroll to bottom of DataGridViewtry this.

滚动到底部DataGridView试试这个。

dataGridView1.FirstDisplayedScrollingRowIndex = dataGridView1.RowCount-1;

回答by Ed Sharp

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 来处理我所有的 DataGridView 项目,这让我可以在任何项目中自由使用语言。我的所有程序都会捕获所有按键,以便我可以将它们用于自己的目的。对于 DataGridView 滚动,我将 PageUp/PageDown 键用于单页,Ctrl/Page 用于单行,Alt/Page 用于顶部 (Up) 和底部 (Down)。C#代码和Basic调用顺序如下:

//---------- 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