1

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:

enter image description here

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. enter image description here

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

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?

enter image description here enter image description here enter image description here

Linesofcode
  • 5,327
  • 13
  • 62
  • 116
  • 1
    AFIK, this is usually done with [Virtual Mode](https://msdn.microsoft.com/en-us/library/15a31akc) – Slai Sep 04 '16 at 17:33

2 Answers2

0

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.

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
}
John Evans Solachuk
  • 1,953
  • 5
  • 31
  • 67
  • If anyone else is seeing only a `Scroll` event handler instead of `EndScroll`, it can do the same thing. Check `ScrollEventArgs e` for `e.Type == ScrollEventType.EndScroll`. – MikeTV Aug 11 '22 at 14:01
-1

Well if i understood you correctly you want for the first time(On first program run) just load only 200 rows not more isn't it ?! If that's so u should use LINQ :), if you are familiar with it u can just write like this : ( I WILL SHOWV U THE PSEUDOCODE) Some Data object".Select(p => p).Take(200) // If u will write similar LINQ request u will get first 200 Data rows from your database.

More here sample u can look at it

var something = someobject.
                     .Select(a => a.name)
                     .Take(200);

P.S. Unfortunately i am not familiar how well u know C# LINQ libraries but anyway u can use google to get more specific info's about LINQ.

Hope this will be little help for you Good luck in coding mate.

  • The question is more concerned with the scrollbar action than with reading more records, I think. – TaW Sep 04 '16 at 18:45
  • He has a problem during the data loading, i guess he does not invoke the data correctly when he is pulling up scroll bar and releasing it.... scollbar is heating down and continues data rows loading, that's why i have advised to use LINQ.......... Guess he is using old style of Data Access Layer(DAL) and if he wants so...... it means he should be more specific in for() loop how to manage loading process. I think he should use LINQ for that it is more convenient and secure. Also I am concerned abour WinForm why he is using WinForms when we have WPF – Achiko Mezvrishvili Sep 05 '16 at 17:04