DataGridView的:自动向下滚动只有当滚动是在底部 [英] DataGridView: Scroll down automatically only if the scroll is at the bottom

查看:642
本文介绍了DataGridView的:自动向下滚动只有当滚动是在底部的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个使用的dataGridView为表明会自动更新每隔一秒,加入行的dataGridView数据的程序。



当我想读围绕年初的东西,我滚动,即使数据更新,滚动条不下去,这是件好事。但我想滚动条往下走,只有当它在DataGridView的底部



我要当一个新的行添加到文本的行为:



如果滚动条是在底部,向下自动滚动显示。
如果滚动条在别处,不滚动



我对这个书面和遗憾的是不工作的代码是:



 私人无效liveDataTable_Scroll(对象发件人,ScrollEventArgs E)
{
的scrollPosition = liveDataTable.FirstDisplayedScrollingRowIndex;

如果(==的scrollPosition liveDataTable.RowCount - 1)
{
IsScrolledToBottom = TRUE;
}
,否则
{
IsScrolledToBottom = FALSE;
}
}
公共无效AddRowToDataGridMethod()
{
dataTable.Rows.Add();

如果(dataWin.IsScrolledToBottom ==真)
dataWin.LiveDataTable.FirstDisplayedScrollingRowIndex =(dataWin.ScrollPosition + 1);
,否则
dataWin.LiveDataTable.FirstDisplayedScrollingRowIndex = dataWin.ScrollPosition;
}


解决方案

您可以试试这个:

  INT firstDisplayed = liveDataTable.FirstDisplayedScrollingRowIndex; 
INT显示= liveDataTable.DisplayedRowCount(真);
INT lastVisible =(firstDisplayed +显示) - 1;
INT lastIndex的= liveDataTable.RowCount - 1;

liveDataTable.Rows.Add(); //添加你行

如果(lastVisible == lastIndex的)
{
liveDataTable.FirstDisplayedScrollingRowIndex = firstDisplayed + 1;
}



所以基本上检查是否最后一行是可见的,如果它是涡旋1行下添加新行之后。


I have a program that uses dataGridView for showing data that updates automatically every second by adding rows to dataGridView.

When I want to read something around the beginning, I scroll up, and even when data updates, the scroll bar doesn't go down, it is good. But I want the scroll bar to go down only when it is at the bottom of dataGridView.

The behavior I want when a new row is added to the text:

if the scrollbar is at the bottom, scroll down automatically. if the scrollbar is elsewhere, don't scroll.

The code I have written for this and unfortunately doesn't work is:

 private void liveDataTable_Scroll(object sender, ScrollEventArgs e)
 {
    ScrollPosition = liveDataTable.FirstDisplayedScrollingRowIndex; 

    if (ScrollPosition == liveDataTable.RowCount - 1)
    {
       IsScrolledToBottom = true;
    }
    else
    {
       IsScrolledToBottom = false;
    }            
 }
 public void AddRowToDataGridMethod()
 {
    dataTable.Rows.Add();

    if (dataWin.IsScrolledToBottom == true)
         dataWin.LiveDataTable.FirstDisplayedScrollingRowIndex = (dataWin.ScrollPosition + 1);
    else
         dataWin.LiveDataTable.FirstDisplayedScrollingRowIndex = dataWin.ScrollPosition;         
 }

解决方案

You can try this:

int firstDisplayed = liveDataTable.FirstDisplayedScrollingRowIndex;
int displayed = liveDataTable.DisplayedRowCount(true);
int lastVisible = (firstDisplayed + displayed) - 1;
int lastIndex = liveDataTable.RowCount - 1;

liveDataTable.Rows.Add();  //Add your row

if(lastVisible == lastIndex)
{
     liveDataTable.FirstDisplayedScrollingRowIndex = firstDisplayed + 1;
}

So basically check if the last row is visible and if it is scroll 1 row down after adding the new row.

这篇关于DataGridView的:自动向下滚动只有当滚动是在底部的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆