通过DataGridView可以到达分段显示数据的效果,即通过设定每次加载数据的最大数量,当滚动条滚动到dataGridView底部时就会继续加载后面一定数量的数据。
类似于ArcMap显示图层的属性表时的效果,它默认起始显示大概2000条数据。
Demo如下(首先在Form中添加一个DataGridView,默认命名为dataGridView1):
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace WindowsApplication30
{
public partial class Form1 : Form
{
private DateTime lastLoading;
private int firstVisibleRow;
private ScrollBars gridScrollBars;
public Form1()
{
InitializeComponent();
//attach scroll event.
dataGridView1.Scroll += new ScrollEventHandler(dataGridView1_Scroll);
//load first 100 rows.
LoadRows();
}
private void HideScrollBars()
{
gridScrollBars = dataGridView1.ScrollBars;
dataGridView1.ScrollBars = ScrollBars.None;
}
private void ShowScrollBars()
{
dataGridView1.ScrollBars = gridScrollBars;
}
private int GetDisplayedRowsCount()
{
int count = dataGridView1.Rows[dataGridView1.FirstDisplayedScrollingRowIndex].Height;
count = dataGridView1.Height / count;
return count;
}
private void LoadRows()
{
HideScrollBars();
System.Diagnostics.Debug.WriteLine("Load data");
lastLoading = DateTime.Now;
//create rows
for (int i = 0; i < 100; i++)
{
int n =dataGridView1.Rows.Add();
dataGridView1.Rows[n].Cells[0].Value = "Row - " + n.ToString();
}
//reset displayed row
if (firstVisibleRow > -1)
{
ShowScrollBars();
dataGridView1.FirstDisplayedScrollingRowIndex = firstVisibleRow;
}
}
void dataGridView1_Scroll(object sender, ScrollEventArgs e)
{
if (e.Type == ScrollEventType.SmallIncrement || e.Type == ScrollEventType.LargeIncrement)
{
if (e.NewValue >= dataGridView1.Rows.Count - GetDisplayedRowsCount())
{
//prevent loading from autoscroll.
TimeSpan ts = DateTime.Now - lastLoading;
if (ts.TotalMilliseconds > 100)
{
firstVisibleRow = e.NewValue;
LoadRows();
}
else
{
dataGridView1.FirstDisplayedScrollingRowIndex = e.OldValue;
}
}
}
}
}
}
原文作者的解释:
private DateTime lastLoading;
In this variable I save last loading time and it is used to prevent "loading while loading" (scroll event is fired while loading because of the automatic scrolling) . Interval between loading in this example is 100 ms.
private int firstVisibleRow;
In this variable I save first visible row index before loading and set datagridview first visible row after loading. This is also used because of the automatic scrolling.
private ScrollBars gridScrollBars;
In this variable I save datagridview scrollbars property and it is used to force scrollbar update because scrollbar is not updated after loading.
//attach scroll event.
dataGridView1.Scroll += new ScrollEventHandler(dataGridView1_Scroll);
In this line I just atach datagridview scroll event.
//load firs 100 rows.
LoadRows();
In this line I load first 100 rows on startup. I use this method because in my test application I don't have any data and this simulate real loading.
每次加载100行数据。在这里使用模拟数据进行展示。
private void HideScrollBars()
{
gridScrollBars = dataGridView1.ScrollBars;
dataGridView1.ScrollBars = ScrollBars.None;
}
In this method I save datagridview scrollbars in variable and hide scrollbars. (update scrollbars after loading).
将datagridview的滑动条保存在变量里并隐藏。便于在重载后,更新滑动条。
private void ShowScrollBars()
{
dataGridView1.ScrollBars = gridScrollBars;
}
In this method I show datagridview scrollbars. (update scrollbars after loading).
在这里,将datagridview的滑动条显示出来。这时,滑动条的位置会根据行数的变化而变化。
private int GetDisplayedRowsCount()
{
int count = dataGridView1.Rows[dataGridView1.FirstDisplayedScrollingRowIndex].Height;
count = dataGridView1.Height / count;
return count;
}
In this method I calculate number of visible rows. This is needed because value in datagridview scroll event (e.NewValue) is index of first visible row in datagridview.
private void LoadRows()
{
HideScrollBars();
System.Diagnostics.Debug.WriteLine("Load data");
lastLoading = DateTime.Now;
//create rows
for (int i = 0; i < 100; i++)
{
int n =dataGridView1.Rows.Add();
dataGridView1.Rows[n].Cells[0].Value = "Row - " + n.ToString();
}
//reset displayed row
if (firstVisibleRow > -1)
{
ShowScrollBars();
dataGridView1.FirstDisplayedScrollingRowIndex = firstVisibleRow;
}
}
In this method I create rows in grid and you should replace for loop with your load data. You need code which is before and after loop.
void dataGridView1_Scroll(object sender, ScrollEventArgs e)
{
if (e.Type == ScrollEventType.SmallIncrement || e.Type == ScrollEventType.LargeIncrement)
{
if (e.NewValue >= dataGridView1.Rows.Count - GetDisplayedRowsCount())
{
//prevent loading from autoscroll.
TimeSpan ts = DateTime.Now - lastLoading;
if (ts.TotalMilliseconds > 100)
{
firstVisibleRow = e.NewValue;
LoadRows();
}
else
{
dataGridView1.FirstDisplayedScrollingRowIndex = e.OldValue;
}
}
}
In this event handler I do few thing:
- Check scroll event type - because we need to load data only in scroll down
- Get new value from event args (e.NewValue) - this is index of first displayed row in grid
- Get displayed rows count - rows in view
- Check if new value is bigger or equal to last row in grid - if yes.. then load more data
- Check last loading time - prevent "loading from loading" (workaround for grid autoscroll)
Reference:Catch scroll to end of DataGridView in C# http://sochinda.wordpress.com/2012/01/03/catch-scroll-to-end-of-datagridview-in-c/