滚动到C#DataGridView的底部
我试图在C#WinForm中滚动到DataGridView的底部 此代码与文本框一起使用:滚动到C#DataGridView的底部,c#,winforms,datagridview,scrollbar,C#,Winforms,Datagridview,Scrollbar,我试图在C#WinForm中滚动到DataGridView的底部 此代码与文本框一起使用: textbox_txt.SelectionStart = textbox_txt.Text.Length; textbox_txt.ScrollToCaret(); 。。。但我不知道如何使用DataGridView来实现。有什么帮助吗?要滚动到DataGridView的底部,请尝试以下操作 dataGridView1.FirstDisplayedScrollingRowIndex = dataGridV
textbox_txt.SelectionStart = textbox_txt.Text.Length;
textbox_txt.ScrollToCaret();
。。。但我不知道如何使用DataGridView来实现。有什么帮助吗?要滚动到
DataGridView
的底部,请尝试以下操作
dataGridView1.FirstDisplayedScrollingRowIndex = dataGridView1.RowCount-1;
作为一名商业程序员,我使用C#DLL来处理我所有的DataGridView项目,这使我可以自由地使用语言来处理我所从事的任何项目。我所有的程序都会捕获所有按键,以便我可以将它们用于我自己的目的。对于DataGridView滚动,我使用PageUp/PageDown键显示单个页面,Ctrl/page键显示单行,Alt/page键显示顶部(向上)和底部(向下)。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
/------------C#Dll部分源代码-----------
公演
{get{返回vu.DisplayedRowCount(false);}
公共整数行数
{get{return vu.RowCount;}}
公共void页面移动(int行)
{
int rowlimit=vu.RowCount-1;
int calc=vu.FirstDisplayedScrollingRowIndex+行;
如果(calc>rowlimit)calc=rowlimit;//转到底部
如果(calc<0)calc=0;//转到顶部
vu.FirstDisplayedScrollingRowIndex=calc;
}
//------结束数据网格视图----------
//----------调用程序C#----------
公共无效页_键(int val,int lastKey)
{
int inc=1;//vu是DataGridView
如果(val==33)inc=-1;
int rowsDisp=vu.RowShow;//#显示的行数
int rowsMax=vu.RowCount;//#视图中的行
int行=0;
开关(最后一把钥匙)
{
案例17://第页之前的Ctrl
rows=inc;
打破
案例19://第页之前的Alt
rows=rowsMax*inc;
打破
违约:
rows=rowsDisp*inc
打破
}//结束开关
vu.PageMove(行)
}//结束页\u键
'----调用程序B4PPC,VB-----
子页_键(val,lastKey)'33=页向上,34=页向下
inc=1'vu为DataGridView
如果val=33,则inc=-1
rowsDisp=vu.RowShow'#显示的行数
rowsMax=vu.RowCount'#视图中的行数
行数=0
选择lastKey
案例17“在第页之前按Ctrl键”
行=公司
案例19“第页之前的Alt”
rows=rowsMax*inc
其他情况
rows=rowsDisp*inc
结束选择
lastKey=“”
vu.PageMove(行)
端接头
可能会复制有用的,谢谢。如果DataGridView有隐藏的行,那么您需要检查行的可见性,因为DataGridView不允许您滚动到不可见的行。很好!如果DataGridView绑定到BindingSource,则可以使用dataGridView1.FirstDisplayedScrollingRowIndex=BindingSource.Count-1获得相同的结果;如果存在当前选定的单元格,则此操作不起作用,因为DataGridView试图使该单元格保持可见。有没有办法强制滚动?我不想更改当前单元格,只需让它从屏幕上滚动即可。