通过DataGridView分段显示数据

通过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/

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 4
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值