• 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());
            }
        }
    }
  • 相关阅读:
    Mysql基础3-数据操作语言DML-数据查询语言DQL
    Mysql基础2-数据定义语言DDL
    Mysql基础1-基础语法-字段类型
    php源码建博客5--建库建表-配置文件-错误日志
    PHP基础4--函数-数组
    php源码建博客4--实现MVC结构微型框架
    PHP基础3--文件加载-错误处理
    php源码建博客3--区分平台的MVC结构
    php源码建博客2--实现单入口MVC结构
    php源码建博客1--搭建站点-实现登录页面
  • 原文地址:https://www.cnblogs.com/jcdd-4041/p/3447869.html
Copyright © 2020-2023  润新知