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:
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.
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?