c#常用表格控件dataGridView的分頁顯示
前言
dataGridView是常用的表格控件,實現(xiàn)分頁的方式也有很多種,例如直接使用sql語言,配合存儲方式,直接讀取某一頁的內(nèi)容,大家如果有興趣的話,下次整理出來與大家分享,本次,采用另一種方式,即控制表格在接收所有數(shù)據(jù)后,不再使用滾輪展示,而是通過控制定位行來實現(xiàn)。
使用該方法需要注意的是,大數(shù)據(jù)量還是不建議使用,因為大數(shù)據(jù)量會導(dǎo)致表格UI卡頓,還是采用sql查詢的方式較為可靠。
下面就簡單的使用控件分頁的方式與大家分享。
1.讓垂直滾動條消失
dataGridView1.ScrollBars =ScrollBars.None;
2.確定每頁顯示多少行
如果表格是固定大小,此步驟可省略,直接用行數(shù)進(jìn)行下面的操作即可,此方法針對表格大小可調(diào)整自適應(yīng)的時候,通過計算獲取能顯示的最大行。
int rowCount;//每一頁多少行 int maxCount;//所有頁數(shù) 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); }
以上方法是針對表格內(nèi)容還沒有添加,如果表格已經(jīng)有內(nèi)容,是無法通過模板(RowTemplate)修改高度的。
那可以使用:
(1)ColumnHeadersHeaderSize屬性設(shè)為 EnableResizing
(2)ColumnHeadersHeader 的值改為 需要的高度
(3)RowTemplate屬性下的Height,把值也設(shè)置為 需要的高度
3.控制表格定位
使用FirstDisplayedScrollingRowIndex屬性調(diào)整顯示內(nèi)容。
dataGridView1.FirstDisplayedScrollingRowIndex = 0;//定位到第一頁 dataGridView1.FirstDisplayedScrollingRowIndex = dataGridView1.Rows.Count- rowCount;//定位到最后一頁
例如:
以上就是簡單利用dataGridView的分頁顯示,總結(jié)來說就是計算每一頁顯示多少行,計算dataGridView每次定位到哪里。
補充:datagridview分頁讀取,定時循環(huán)翻頁
在VB中使用C# 語言,datagridview和計時器。
這里我假設(shè)datagridview一頁顯示五行,兩秒變換一次。
拖一個時間控件到頁面上,設(shè)置Interval屬性為2000.
我寫好了注釋和流程,大家都能看懂的
using System.Data; using System.Data.SqlClient;//使用到這個類,先添加命名空間,訪問數(shù)據(jù)庫 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;//總行數(shù) private int PageCount = 0;//滿頁頁數(shù) private int residualRowsCount = 0;//除去滿頁,最后一頁的數(shù)據(jù),余數(shù) private int TakeCount = 0;//當(dāng)前頁頁數(shù) public Form1() { InitializeComponent(); } //public string myConnectString = "server=192.168.50.50;uid=sa;pwd=123;database=111"; //使用IP地址連接數(shù)據(jù)庫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數(shù)據(jù)源 initial catalog數(shù)據(jù)庫 Persist Security Info是否保存安全信息 SqlConnection myConn = new SqlConnection(myConnectString); //創(chuàng)建數(shù)據(jù)庫連接對象 myConn.Open(); //打開數(shù)據(jù)庫 SqlCommand myComm = new SqlCommand("select * from Table_1 order by ID asc", myConn); //用sql語句實現(xiàn)查詢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中的數(shù)據(jù)行數(shù) PageCount = DataRowsCountTotal / 5; //求商 PageCount滿頁的頁數(shù) residualRowsCount = DataRowsCountTotal % 5; //取余residualRowsCount最后一頁的數(shù)據(jù)行數(shù) 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); //位置坐標(biāo) label2.Text = DateTime.Now.ToLongTimeString(); //顯示當(dāng)前的時間 label2.Location = new Point(a, this.Size.Height-30); //位置最下,居中 dataGridView1.AutoGenerateColumns = false; //不允許bai自動創(chuàng)建列, dataGridView1.RowHeadersVisible = false;//刪除最左邊一列,把控件的 RowHeadVisible屬性設(shè)置為false this.FormBorderStyle = FormBorderStyle.None; //無邊框 this.WindowState = FormWindowState.Maximized; //窗體最大化 //dataGridView1.Dock = DockStyle.Fill; //控件最大化 dataGridView1.AllowUserToAddRows = false; //不同意用戶添加行,這樣就不會出現(xiàn)最后一行空白行,大多數(shù)時候表格只是用來展示數(shù)據(jù)而非用戶錄入數(shù)據(jù)(錄入數(shù)據(jù)神馬的用EXCEL更方便吧) /* 設(shè)置屬性AutoSizeColumnsMode = Fill;列表頭的寬度均勻分配,填滿表格空間。 設(shè)置屬性BackgroundColor = White; 背景色設(shè)置為白色 timer1.Interval = 2000; 定時器間隔時間 */ //toolStripStatusLabel1.Text = "登錄用戶:" + Form1.strName;//顯示登錄用戶,顯示登錄時間 } /* DataSet.Tables[0].Rows[0][1]表示DataSet中第一張表(因為Tables[0]就是第一張表的意思)中第一行(Rows[0][]) 第二列(Rows[][1])的數(shù)據(jù)。 DataSet.Tables["tableName"] 是指定獲取特定的表名。如果DataSet只有一張表,則為DataSet.Tables[0]. */ private void DataGrieDataBind(int TakeCount) { DataTable myDt = new DataTable(); //創(chuàng)建一個DataTable的對象,虛擬表 myDt = myDataSet.Tables[0].Clone(); //克隆表 // myDt.Clear();//清除行信息為0 if (TakeCount + 1 > PageCount)//如果 當(dāng)前頁數(shù) >滿頁頁數(shù),即最后一頁 { for (int i = 5 * TakeCount; i <= DataRowsCountTotal - 1; i++) {// i=5n-1——總行數(shù)-1,i++ myDt.ImportRow(myDataSet.Tables[0].Rows[i]);//顯示第一張表,第i行的數(shù)據(jù) } } else //否則執(zhí)行 { for (int i = 5 * TakeCount; i <= 5 * TakeCount + 4; i++) {//i=5n——5n+4, myDt.ImportRow(myDataSet.Tables[0].Rows[i]);//顯示數(shù)據(jù) } } myDt.AcceptChanges(); this.dataGridView1.DataSource = myDt; // 為dataGridView1指定數(shù)據(jù)源 } private void timer1_Tick(object sender, EventArgs e) {//定時器循環(huán)翻頁 TakeCount = TakeCount + 1;//翻頁 // MessageBox.Show(TakeCount.ToString()); if (TakeCount > PageCount)//如果 當(dāng)前頁 >滿頁頁數(shù) { TakeCount = 0;//顯示第一頁 } DataGrieDataBind(TakeCount); } private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e) { } } } 邏輯結(jié)構(gòu)大概是這個 private int DataRowsCountTotal = 0;//總行數(shù) private int PageCount = 0;//滿頁頁數(shù) private int residualRowsCount = 0;//除去滿頁,最后一頁的數(shù)據(jù),余數(shù) private int TakeCount = 0;//當(dāng)前頁頁數(shù) DataRowsCountTotal = myDataSet.Tables[0].Rows.Count;//=MyDataSet中的數(shù)據(jù)行數(shù) PageCount = DataRowsCountTotal / 5; //求商 PageCount滿頁的頁數(shù) residualRowsCount = DataRowsCountTotal % 5; //取余residualRowsCount最后一頁的數(shù)據(jù)行數(shù)`` 第幾行 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
總結(jié)
到此這篇關(guān)于c#常用表格控件dataGridView分頁顯示的文章就介紹到這了,更多相關(guān)c# dataGridView分頁顯示內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Visual Stodio2022中沒有mysql.dll的解決辦法
這篇文章介紹了Visual Stodio2022中沒有mysql.dll的解決辦法,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2021-12-12C#中判斷本地系統(tǒng)的網(wǎng)絡(luò)連接狀態(tài)的方法
C#中一般通過InternetGetConnectedState函數(shù)返回本地系統(tǒng)的網(wǎng)絡(luò)連接狀態(tài),下面簡單介紹下,需要的朋友可以參考下2013-10-10C#中Trim()、TrimStart()、TrimEnd()的用法介紹
這篇文章主要介紹了C#中Trim()、TrimStart()、TrimEnd()的用法,有需要的朋友可以參考一下2014-01-01