tags:

views:

475

answers:

3
+2  Q: 

Scroll Event in C#

Hello all,

Let's say I have a DataGridView Control that displays about 100 records of a specified table. Every time a user scrolls down to the lowest point, I want to add another 100 records to the DataSet that stands behind my DataGridView.

I knwo that a DataGridView Control has a Scroll Event like private void DataGridView_Scroll(object sender, ScrollEventArgs e), and that the ScrollEventArgs give me the ScrollOrientation and the ScrollEventType. But how exactly do I figure out exactly when the scroll has reached the end of the scroll bar?

Tnks.

A: 

It sounds like you might want to look at "virtual mode". The data binding works differently, but it is well suited to lazy data fetching.

Marc Gravell
I don't see how the <Code>Virtual Mode</Code> property could help me... I just need to find out when a user has scrolled down to the very last record in the <Code>DataGridView</Code>.
Bogdan M
Because as you scroll, it asks you to provide more data... isn't that exactly what you want to achieve?
Marc Gravell
I understand now what you mean.If this is what I want to achieve, well, not exactly. Just before I will add more data in the DataGridView, I want to display a MessageBox, and probably do more stuff, all of this exactly when the scroll button has reached the end of the scroll bar.Interesting ideea though, I keep it in mind for cases with a better application design.Thanks.
Bogdan M
A: 

Every DataGridViewRow has a "Displayed" property that indicates whether or not the row is currently being shown on screen. Therefore, you can do something like this:

if(this.dataGridView1.Rows[this.dataGridView1.Rows.Count - 1].Displayed)
{
   //last rows is displayed, do what you gotta do
}
BFree
Not exacly what I have in mind (because the last row can be visible even if the scroll button has not reached the end of the scroll bar), but it solves my problem. Unless someone comes with a better ideea in the next 5 minutes, I am going to mark your post as an answer. Thanks.
Bogdan M
A: 

Use a DataReadear to read some records. Then in the Scroll event of the DataGridView, check to see if the property FirstDisplayedScrollingRowIndex is getting close to the count of loaded records, use the DataReader again to fetch another bunch of rows and so on.

kaze