網站首頁 編程語言 正文
前言
dataGridView是常用的表格控件,實現分頁的方式也有很多種,例如直接使用sql語言,配合存儲方式,直接讀取某一頁的內容,大家如果有興趣的話,下次整理出來與大家分享,本次,采用另一種方式,即控制表格在接收所有數據后,不再使用滾輪展示,而是通過控制定位行來實現。
使用該方法需要注意的是,大數據量還是不建議使用,因為大數據量會導致表格UI卡頓,還是采用sql查詢的方式較為可靠。
下面就簡單的使用控件分頁的方式與大家分享。
1.讓垂直滾動條消失
dataGridView1.ScrollBars =ScrollBars.None;
2.確定每頁顯示多少行
如果表格是固定大小,此步驟可省略,直接用行數進行下面的操作即可,此方法針對表格大小可調整自適應的時候,通過計算獲取能顯示的最大行。
int rowCount;//每一頁多少行
int maxCount;//所有頁數
private void uc_table_Resize(object sender, EventArgs e)
{
this.Invalidate();
rowCount = (dataGridView1.Height - dataGridView1.ColumnHeadersHeight) / dataGridView1.RowTemplate.Height;
maxCount = (int)Math.Ceiling(dataGridView1.Rows.Count / rowCount * 1.0);
}
以上方法是針對表格內容還沒有添加,如果表格已經有內容,是無法通過模板(RowTemplate)修改高度的。
那可以使用:
(1)ColumnHeadersHeaderSize屬性設為 EnableResizing
(2)ColumnHeadersHeader 的值改為 需要的高度
(3)RowTemplate屬性下的Height,把值也設置為 需要的高度
3.控制表格定位
使用FirstDisplayedScrollingRowIndex屬性調整顯示內容。
dataGridView1.FirstDisplayedScrollingRowIndex = 0;//定位到第一頁
dataGridView1.FirstDisplayedScrollingRowIndex = dataGridView1.Rows.Count- rowCount;//定位到最后一頁
例如:
以上就是簡單利用dataGridView的分頁顯示,總結來說就是計算每一頁顯示多少行,計算dataGridView每次定位到哪里。
補充:datagridview分頁讀取,定時循環翻頁
在VB中使用C# 語言,datagridview和計時器。
這里我假設datagridview一頁顯示五行,兩秒變換一次。
拖一個時間控件到頁面上,設置Interval屬性為2000.
我寫好了注釋和流程,大家都能看懂的
using System.Data;
using System.Data.SqlClient;//使用到這個類,先添加命名空間,訪問數據庫
using System.Drawing;
using System.Text;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Xml;
namespace WindowsFormsApp1
{
public partial class Form1 : Form
{
private DataSet myDataSet = new DataSet();
private int DataRowsCountTotal = 0;//總行數
private int PageCount = 0;//滿頁頁數
private int residualRowsCount = 0;//除去滿頁,最后一頁的數據,余數
private int TakeCount = 0;//當前頁頁數
public Form1()
{
InitializeComponent();
}
//public string myConnectString = "server=192.168.50.50;uid=sa;pwd=123;database=111"; //使用IP地址連接數據庫192.168.50.50
private void Form1_Load(object sender, EventArgs e)
{
string myConnectString = "Server=AUTOBVT-EMECSND;User Id=sa;Pwd=123;DataBase=111;Persist Security Info=True";
// "Persist Security Info=True";// Data Source數據源 initial catalog數據庫 Persist Security Info是否保存安全信息
SqlConnection myConn = new SqlConnection(myConnectString); //創建數據庫連接對象
myConn.Open(); //打開數據庫
SqlCommand myComm = new SqlCommand("select * from Table_1 order by ID asc", myConn);
//用sql語句實現查詢SELECT * FROM 表名 WHERE ID NOT IN (SELECT TOP(I) ID FROM 表名)
SqlDataAdapter myAdap = new SqlDataAdapter();
myAdap.SelectCommand = myComm;
myAdap.Fill(myDataSet, "Table_1");
DataRowsCountTotal = myDataSet.Tables[0].Rows.Count;//=MyDataSet中的數據行數
PageCount = DataRowsCountTotal / 5; //求商 PageCount滿頁的頁數
residualRowsCount = DataRowsCountTotal % 5; //取余residualRowsCount最后一頁的數據行數
DataGrieDataBind(0);
timer1.Enabled = true;//計時器是否正在運行
int a = (this.Size.Width - label1.Size.Width) / 2; //控件的水平居中位置
//int b = (this.Size.Height - 2 * label1.Size.Height) / 2 ;//控件的垂直居中位置
label1.Location = new Point(a, 1); //位置坐標
label2.Text = DateTime.Now.ToLongTimeString(); //顯示當前的時間
label2.Location = new Point(a, this.Size.Height-30); //位置最下,居中
dataGridView1.AutoGenerateColumns = false; //不允許bai自動創建列,
dataGridView1.RowHeadersVisible = false;//刪除最左邊一列,把控件的 RowHeadVisible屬性設置為false
this.FormBorderStyle = FormBorderStyle.None; //無邊框
this.WindowState = FormWindowState.Maximized; //窗體最大化
//dataGridView1.Dock = DockStyle.Fill; //控件最大化
dataGridView1.AllowUserToAddRows = false; //不同意用戶添加行,這樣就不會出現最后一行空白行,大多數時候表格只是用來展示數據而非用戶錄入數據(錄入數據神馬的用EXCEL更方便吧)
/* 設置屬性AutoSizeColumnsMode = Fill;列表頭的寬度均勻分配,填滿表格空間。
設置屬性BackgroundColor = White; 背景色設置為白色
timer1.Interval = 2000; 定時器間隔時間 */
//toolStripStatusLabel1.Text = "登錄用戶:" + Form1.strName;//顯示登錄用戶,顯示登錄時間
}
/* DataSet.Tables[0].Rows[0][1]表示DataSet中第一張表(因為Tables[0]就是第一張表的意思)中第一行(Rows[0][]) 第二列(Rows[][1])的數據。
DataSet.Tables["tableName"] 是指定獲取特定的表名。如果DataSet只有一張表,則為DataSet.Tables[0]. */
private void DataGrieDataBind(int TakeCount)
{
DataTable myDt = new DataTable(); //創建一個DataTable的對象,虛擬表
myDt = myDataSet.Tables[0].Clone(); //克隆表
// myDt.Clear();//清除行信息為0
if (TakeCount + 1 > PageCount)//如果 當前頁數 >滿頁頁數,即最后一頁
{
for (int i = 5 * TakeCount; i <= DataRowsCountTotal - 1; i++)
{// i=5n-1——總行數-1,i++
myDt.ImportRow(myDataSet.Tables[0].Rows[i]);//顯示第一張表,第i行的數據
}
}
else //否則執行
{
for (int i = 5 * TakeCount; i <= 5 * TakeCount + 4; i++)
{//i=5n——5n+4,
myDt.ImportRow(myDataSet.Tables[0].Rows[i]);//顯示數據
}
}
myDt.AcceptChanges();
this.dataGridView1.DataSource = myDt; // 為dataGridView1指定數據源
}
private void timer1_Tick(object sender, EventArgs e)
{//定時器循環翻頁
TakeCount = TakeCount + 1;//翻頁
// MessageBox.Show(TakeCount.ToString());
if (TakeCount > PageCount)//如果 當前頁 >滿頁頁數
{
TakeCount = 0;//顯示第一頁
}
DataGrieDataBind(TakeCount);
}
private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
{ }
}
}
邏輯結構大概是這個
private int DataRowsCountTotal = 0;//總行數
private int PageCount = 0;//滿頁頁數
private int residualRowsCount = 0;//除去滿頁,最后一頁的數據,余數
private int TakeCount = 0;//當前頁頁數
DataRowsCountTotal = myDataSet.Tables[0].Rows.Count;//=MyDataSet中的數據行數
PageCount = DataRowsCountTotal / 5; //求商 PageCount滿頁的頁數
residualRowsCount = DataRowsCountTotal % 5; //取余residualRowsCount最后一頁的數據行數``
第幾行 0 0-4 1頁 1-5
1 5-9 2頁 6-10
2 10-14
n頁 5n/5n+4 n+1頁 5n+1/5n+5
尾頁 5n/N 尾頁+1
總結
原文鏈接:https://blog.csdn.net/Yyuanyuxin/article/details/127445316
相關推薦
- 2022-04-24 教你使用mongoose實現多集合關聯查詢_MongoDB
- 2021-12-21 Number的常見使用方法
- 2022-07-10 pdb時區問題:與當前時間不一致
- 2023-05-30 模型訓練時GPU利用率太低的原因及解決_python
- 2023-01-09 python自動化測試中裝飾器@ddt與@data源碼深入解析_python
- 2022-08-15 基于FTP協議的文件上傳與下載
- 2022-05-12 Scss 遍歷之批量設置樣式
- 2022-04-24 Android掛斷電話最新實現方法_Android
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支