C#在datagridview滚动条上加载行到达末尾 [英] C# load rows on datagridview scroll bar reach end

查看:427
本文介绍了C#在datagridview滚动条上加载行到达末尾的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我使用鼠标将滚动条移动到datagridview的底部时,我遇到的问题是只会发生。因此,当我单击此按钮时,不会出现问题

I'm facing an issue that only happens when I use my mouse to move the scrollbar to the bottom of the datagridview. So, the problem does not occur when I click in this button:

我的目标是在开始时加载200行,然后当用户到达datagridview滚动条的末尾时,再加载200行,依此类推。

My goal is to load 200 rows at beginning and then when user reaches the end of the datagridview scroll bar load more 200 rows and so on..

private void Home_load(object sender, EventArgs e)
{
   LoadRows();
}

// Detects if the scrollbar is at bottom
private void dataGridViewUsers_Scroll(object sender, ScrollEventArgs e)
{
    if (hasMoreRows == false)
        return;

    // The rows height is always 22
    int totalHeight = this.dataGridViewUsers.Rows.Count * 22;

    if ((totalHeight - this.dataGridViewUsers.Height) < this.dataGridViewUsers.VerticalScrollingOffset)
        LoadRows();
}

private void LoadRows()
{
    DataSet DSet = controllers.Users.LazyLoad(offset);

    if (DSet.Tables[0].Rows.Count <= 0)
    {
        hasMoreRows = false;
        return;
    }

    for (int i = 0; i < DSet.Tables[0].Rows.Count; i++)
    {
        this.dataGridViewUsers.Rows.Add
        (
            DSet.Tables[0].Rows[i]["id"].ToString(),
            DSet.Tables[0].Rows[i]["name"].ToString(),
            DSet.Tables[0].Rows[i]["mobile"].ToString(),
            DSet.Tables[0].Rows[i]["signup_date"].ToString()
        );
    }

    offset += 200;
    this.dataGridViewUsers.Refresh();
}

通常发生的事情是人们抓住滚动条并将其拉到底部。

What happens is that usually people grab the scrollbar and pulls it to the bottom.

如果我在datagridview中执行此操作,则会按预期添加更多行,但行为异常发生。

And if I do that within my datagridview, more rows are added as expected but a strange behavior happens.

我大约有2000列,每当我一次将滚动条拉到底部时,我的滚动条就会发疯了。向下,向上和向下向下,向上和向下向下,直到达到总行数。就像滚动条本身是有生命的..还是处于循环中。.

I have roughly 2000~ rows, and whenever I pull the scrollbar into the bottom at once, my scrollbar goes nuts Up & Down, Up & Down, Up & Down, until it reaches the total rows. It's like the scrollbar has life itself..or is in a loop..

你们能在我的代码中看到什么能重现这种行为吗?

Can you guys see in my code what could reproduce such behavior?



推荐答案

我通过处理 EndScroll 事件解决了该问题datagridview的 ScrollBar 控件,而不是datagridview的 Scroll 事件。因此,只有当该人释放滚动条时,系统才会检查它是否在datagridview的底部,并进行适当的处​​理。

I resolved it by handling the EndScroll event of the datagridview's ScrollBar control instead of the datagridview's Scroll event. So only when the person releases the scrollbar, the system will do the check on whether it is at the bottom of the datagridview or not and proceed appropriately.

您可以参考答案有关如何访问datagridview ScrollBar 的事件,而不是处理 Scroll 事件,只需处理 EndScroll 像这样:

You can refer to this answer on how to access the datagridview ScrollBar's event and instead of handling Scroll event, just handle the EndScroll like this:

using System.linq;
public MyFormConstructor()
{
    InitializeComponent();
    VScrollBar scrollBar = dgv.Controls.OfType<VScrollBar>().First();
    scrollBar.EndScroll += MyEndScrollEventHandler;
}

private void MyEndScrollEventHandler(object sender, ScrollEventArgs e)
{
   // Handler with e.Type set properly
}

这篇关于C#在datagridview滚动条上加载行到达末尾的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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