如何使用C#自动向下滚动数据网格框? [英] How to scroll down automatically a datagridview box with C#?

查看:177
本文介绍了如何使用C#自动向下滚动数据网格框?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,



我有一个应用程序将用作公共显示屏,以更新公众正在进行的过程。应用程序连接到sql数据库,几乎只传回信息。应用程序将针对数据库进行自我更新。安装后不需要触摸应用程序。所以不需要按下滚动条,实际上滚动条不应该是可见的。



行数可能会改变,因为还有另一个单独的应用程序运营商可以更新数据库中的信息。所以行数可以是20到100甚至更多,但我的公共屏幕(我使用datagridview来显示数据)应该一次只显示20行。我需要做的是找出一种方法,如果数据网格中有超过20行,网格自动向下滚动(平滑),同时冻结标题行,而不是从一行跳到下一行,也许可以备份或者再次从顶部开始。就像在本文中一样:自动滚动富文本/文本框 [ ^ ]



只要滚动顺畅,也可以使用另一种方法。我正在使用Windows窗体,但也欢迎使用WPF解决方案。数据库是SQL。



几乎系统需要从列表的顶部到底部循环缓慢而平稳地在顶部暂停。我发现的大多数解决方案直接跳到最底行。



我不知道这是否可能,以为我会问?

非常感谢您的帮助。



我的尝试:



我试图使用datagridview当前选择的索引比增加一个,但这显然不是我想要的解决方案。

我在论坛上看到了一些建议增加datagridview FirstDisplayedScrollingRowIndex,但这里也很顺利。

有人建议使用面板并滚动该面板,如何实现?

Hi guys,

I have a application that is going to be used as a public display screen, to update the public about the process that are ongoing. The application connects to sql database and just pretty much relays information back. The application will self-update against the Database. The application shouldn’t need to be touched once installed. So no need to press down the scroll bar, in fact the scroll bars shouldn't be visible.

The number of rows could change, as there is another separate application that operators can update information in the database. So the number of rows could be anything from 20 to 100 or even more but my public screen (I am using a datagridview to display data) should only display 20 rows at a time. what I need to do is figure out a way if there are more than 20 rows in datagrid for the Grid to auto scroll downwards (smoothly) while freezing the header row and not to jump from one row to the next and maybe back up or to start from the top again. Just like in this article: Scrolling a Rich/Textbox Automatically[^]

Another method could also be used as long as the scrolling is smooth. I am using windows forms, but a WPF solution is also welcome. Database is SQL.

Pretty much the system needs to cycle from top to bottom of the list slowly and smoothly pausing at the top. Most solutions I have found jump straight to the bottom row.

I have no idea if this is possible so thought I would ask?
Thank you very much for any kind assistance.

What I have tried:

I tried to use datagridview current selected index than increment it by one, but this clearly is not my desired solution.
I saw in the forum some suggestions to increment datagridview FirstDisplayedScrollingRowIndex, but here also it will luck the smoothness.
Some suggested to use a panel and scroll that panel, How can that be achieved?

推荐答案

private static void EnsureVisibleRow(DataGridView view, int rowToShow)
{
    if (rowToShow >= 0 && rowToShow < view.RowCount)
    {
        var countVisible = view.DisplayedRowCount(false);
        var firstVisible = view.FirstDisplayedScrollingRowIndex;
        if (rowToShow < firstVisible)
        {
            view.FirstDisplayedScrollingRowIndex = rowToShow;
        }
        else if (rowToShow >= firstVisible + countVisible)
        {
            view.FirstDisplayedScrollingRowIndex = rowToShow - countVisible + 1;
        }
    }
}





但是,请记住,当你这样做时,添加新行并试图保持最后一行添加可见会影响用户导航控件的能力。



我认为你可能最好不要显示添加的最后一项作为单独的文本框或沿着该行的某些内容。



However, keep in mind when you do this, that adding new rows and trying to keep the last row added visible will interfere with the user's ability to navigate the control.

I think you might be better off showing the last item added as a separate textbox or something along that line.


只有在从顶部滚动到最后一行然后返回顶部之后才会刷新datagridview以添加新数据。
The datagridview will only be refreshed to add new data after it has scrolled from top to last row then back to top.


这篇关于如何使用C#自动向下滚动数据网格框?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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