winform 监视DataGridView的滚动条,加载数据

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using System.Diagnostics;
using System.IO;
using System.Data.SqlClient;

namespace temp
{
    public partial class ttt : Form
    {
        public ttt()
        {
            InitializeComponent();
        }

        SqlConnection conn = new SqlConnection("Data Source=.;Initial Catalog=ManageDatas;integrated security=true;");        
                
        int index = 1;
        /// <summary>
        /// 是否继续加载数据
        /// </summary>
        bool getDataContinue = true;

        private void ttt_Load(object sender, EventArgs e)
        {
            //this.WindowState = FormWindowState.Maximized;
            do
            {
                AddNew();
            } while (Verify());
        }

        private void button1_Click(object sender, EventArgs e)
        {
            
        } 

        /// <summary>
        /// 加载数据
        /// </summary>
        void AddNew()
        {
            DataTable newData = GetData(index);
            if (newData != null && newData.Rows.Count > 0)
            {
                if (dataGridView1.Columns.Count == 0)
                    for (int i = 0; i < newData.Columns.Count; i++)
                        dataGridView1.Columns.Add(newData.Columns[i].ColumnName, newData.Columns[i].ColumnName);
                foreach (DataRow dr in newData.Rows)
                {
                    int newIndex = dataGridView1.Rows.Add();
                    for (int i = 0; i < dr.ItemArray.Count(); i++)
                        dataGridView1.Rows[newIndex].Cells[i].Value = dr[i];
                }
            }
            else
                getDataContinue = false;
        }
        
        /// <summary>
        /// 获取指定页数据
        /// </summary>
        DataTable GetData(int page,int size=20)
        {
            string sqlstr = string.Format(@"WITH TB AS (SELECT ROW_NUMBER()OVER(ORDER BY intid ASC) AS rowid,* FROM tblbuyermsg)
SELECT * FROM TB  WHERE rowid between {0} and {1}", (page - 1) * size + 1, page * size);
            SqlDataAdapter da = new SqlDataAdapter(sqlstr, conn);
            DataTable dt = new DataTable();
            da.Fill(dt);
            index++;
            return dt;
        }

        /// <summary>
        /// 验证是否需要加载数据
        /// </summary>
        /// <returns>需要加载则返回true</returns>
        bool Verify()//gridView高度不能太低
        {
            if (!getDataContinue)
                return false;
            bool bottom = false;
            //若gridView高度太低,则考虑
            if (dataGridView1.Height < 40 && dataGridView1.FirstDisplayedScrollingRowIndex > dataGridView1.Rows.Count - 3)
                bottom = true;
            else
            {
                int rowsHeight = 0;
                for (int i = dataGridView1.FirstDisplayedScrollingRowIndex; i < dataGridView1.Rows.Count; i++)
                {
                    //if (i < 0) i = 0;
                    rowsHeight += dataGridView1.Rows[i].Height;
                    if (rowsHeight - dataGridView1.Height > 30)
                    {
                        return false;
                    }
                }
                bottom = true;
            }
            return bottom;
        }
        
        private void dataGridView1_Scroll(object sender, ScrollEventArgs e)
        {
            if (Verify())
                AddNew();
        }

        private void dataGridView1_Resize(object sender, EventArgs e)
        {
            do
            {
                AddNew();
            } while (Verify());
        }
    }
}

 

转载于:https://www.cnblogs.com/jcdd-4041/p/3447869.html

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值