您好,登錄后才能下訂單哦!
在C# 中使用datagridview控件時顯示大量數(shù)據(jù)拖拉卡頓如何解決?針對這個問題,這篇文章詳細(xì)介紹了相對應(yīng)的分析和解答,希望可以幫助更多想解決這個問題的小伙伴找到更簡單易行的方法。
由于在使用SQL查詢大量的數(shù)據(jù)并一次顯示到dataGridView控件,出現(xiàn)拖拉的時候卡頓。
解決方法:
1.首先分頁。
2.其次把顯示控件設(shè)置雙buffer。
1.設(shè)置dataGridView雙buffer代碼如下,需要引用反射命名空間
Type dgvType = this.dataGridView1.GetType(); PropertyInfo pi = dgvType.GetProperty("DoubleBuffered", BindingFlags.Instance | BindingFlags.NonPublic); pi.SetValue(this.dataGridView1, true, null);
2.自己定義分頁操作控件,上一頁,下一頁,首頁,尾頁,在加載form的時候添加事件。并為他們寫一個操作事件
private void Form1_Load(object sender, EventArgs e) { this.btn_EndPage.Click += Ctrl_Click; this.btn_FirstPage.Click += Ctrl_Click; this.btn_LastPage.Click += Ctrl_Click; this.btn_NextPage.Click += Ctrl_Click; //this.SetStyle(ControlStyles.OptimizedDoubleBuffer | ControlStyles.ResizeRedraw | ControlStyles.AllPaintingInWmPaint, true); //this.UpdateStyles(); } private void Ctrl_Click(object sender, EventArgs e) { Button btn = (Button)sender; if (btn.Text == this.btn_EndPage.Text) { if (currentPage == pageCount) { return; } currentPage = pageCount; LoadPage(); } else if (btn.Text == this.btn_FirstPage.Text) { if (currentPage == 1) { return; } currentPage = 1; LoadPage(); } else if (btn.Text == this.btn_LastPage.Text) { if (currentPage == 1) { return; } currentPage--; LoadPage(); } else if (btn.Text == this.btn_NextPage.Text) { if (currentPage == pageCount) { return; } currentPage++; LoadPage(); } else { MessageBox.Show("error"); } }
3.下面代碼是分頁方法,加載page
#region datagridview sort /// <summary> /// 每頁記錄數(shù) /// </summary> private int pageSize = 50; /// <summary> /// 總記錄數(shù) /// </summary> private int recordCount = 0; /// <summary> /// 總頁數(shù) /// </summary> private int pageCount = 0; /// <summary> /// 當(dāng)前頁 /// </summary> private int currentPage = 0; /// <summary> /// 分頁的方法 /// </summary> /// <param name="str"></param> private void PageSorter() { foreach (DataColumn col in dt.Columns) { DataGridViewTextBoxColumn dgvc = new DataGridViewTextBoxColumn(); dgvc.Name = ""; dgvc.DataPropertyName = ""; dgvc.HeaderText = col.ToString(); dataGridView1.Columns.Add(dgvc); } recordCount = dt.Rows.Count; //記錄總行數(shù) pageCount = (recordCount / pageSize); if ((recordCount % pageSize) > 0) { pageCount++; } //默認(rèn)第一頁 currentPage = 1; LoadPage();//調(diào)用加載數(shù)據(jù)的方法 } /// <summary> /// LoadPage方法 /// </summary> private void LoadPage() { if (currentPage < 1) currentPage = 1; if (currentPage > pageCount) currentPage = pageCount; int beginRecord; //開始指針 int endRecord; //結(jié)束指針 DataTable dtTemp; dtTemp = dt.Clone(); beginRecord = pageSize * (currentPage - 1); if (currentPage == 1) beginRecord = 0; endRecord = pageSize * currentPage; if (currentPage == pageCount) endRecord = recordCount; for (int i = beginRecord; i < endRecord; i++) { dtTemp.ImportRow(dt.Rows[i]); } dataGridView1.Rows.Clear(); this.lb_CurrentRow.Text = "當(dāng)前頁: " + currentPage.ToString() + " / " + pageCount.ToString();//當(dāng)前頁 this.lb_TotalRows.Text = "總行數(shù): " + recordCount.ToString() + " 行";//總記錄數(shù) 把臨時table的數(shù)據(jù)插入到datagridview控件里面。 for (int i = 0; i < dtTemp.Rows.Count; i++) { dataGridView1.Rows.Add(); for (int j = 0; j < dtTemp.Columns.Count; j++) dataGridView1.Rows[i].Cells[j].Value = dtTemp.Rows[i][j].ToString(); } } #endregion
Tips:
文中的“dt”是datatable類型的數(shù)據(jù),給位看官可以自己創(chuàng)建,也可以從數(shù)據(jù)庫直接獲取測試數(shù)據(jù)。
1.設(shè)置dataGridView自增序列號
//填充序列號 private void dataGridView1_RowPostPaint_1(object sender, DataGridViewRowPostPaintEventArgs e) { Rectangle rectangle = new Rectangle(e.RowBounds.Location.X, e.RowBounds.Location.Y, dataGridView1.RowHeadersWidth - 4, e.RowBounds.Height); TextRenderer.DrawText(e.Graphics, (e.RowIndex + 1).ToString(), dataGridView1.RowHeadersDefaultCellStyle.Font, rectangle, dataGridView1.RowHeadersDefaultCellStyle.ForeColor, TextFormatFlags.VerticalCenter | TextFormatFlags.Right); }
1.設(shè)置dataGridView自增序列號
//用委托更新data,防止卡頓。 private delegate void UpdateDataGridView(DataTable dt); private void UpdateGV(DataTable dt) { if (dataGridView1.InvokeRequired) { //這里沒有看懂,有大神可以解釋下嘛 this.BeginInvoke(new UpdateDataGridView(UpdateGV), new object[] { dt }); } else { dataGridView1.DataSource = dt; dataGridView1.Refresh(); } } }
關(guān)于在C# 中使用datagridview控件時顯示大量數(shù)據(jù)拖拉卡頓如何解決問題的解答就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,如果你還有很多疑惑沒有解開,可以關(guān)注億速云行業(yè)資訊頻道了解更多相關(guān)知識。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。