zoukankan      html  css  js  c++  java
  • 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());
            }
        }
    }
  • 相关阅读:
    再次或多次格式化导致namenode的ClusterID和datanode的ClusterID之间不一致的问题解决办法
    Linux安装aria2
    POJ 3335 Rotating Scoreboard 半平面交
    hdu 1540 Tunnel Warfare 线段树 区间合并
    hdu 3397 Sequence operation 线段树 区间更新 区间合并
    hud 3308 LCIS 线段树 区间合并
    POJ 3667 Hotel 线段树 区间合并
    POJ 2528 Mayor's posters 贴海报 线段树 区间更新
    POJ 2299 Ultra-QuickSort 求逆序数 线段树或树状数组 离散化
    POJ 3468 A Simple Problem with Integers 线段树成段更新
  • 原文地址:https://www.cnblogs.com/jcdd-4041/p/3447869.html
Copyright © 2011-2022 走看看