滚动到C#DataGridView的底部

时间:2012-04-01 22:53:24

标签: c# winforms datagridview scrollbar

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

此代码适用于TextBox:

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

...但我不知道如何使用DataGridView。有什么帮助吗?

2 个答案:

答案 0 :(得分:74)

要滚动到DataGridView的底部,请尝试此操作。

dataGridView1.FirstDisplayedScrollingRowIndex = dataGridView1.RowCount-1;

答案 1 :(得分:3)

作为一名商业程序员,我使用C#DLL处理我的所有DataGridView项目,这使我能够为我承担的任何项目提供语言自由。我的所有程序都会捕获所有按键,以便我可以将它们用于我自己的目的。对于DataGridView滚动,我使用PageUp / PageDown键作为单页,Ctrl / Page作为单行,Alt / Page作为top(Up)和bottom(Down)。 C#代码和基本调用顺序如下:

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