您好,登錄后才能下訂單哦!
本篇文章給大家分享的是有關怎么在DataGridView中使用BindingNavigator實現一個分頁功能,小編覺得挺實用的,因此分享給大家學習,希望大家閱讀完這篇文章后可以有所收獲,話不多說,跟著小編一起來看看吧。
實現方法如下:
1、新建一個WinForm程序,命名為BindingNavigatorMain,并拖入一個DataGridView控件及一個BindingNavigator控件。在BindingNavigator右下角彈窗中添加
一個Button(轉到),BindingNavigator的樣式如下:
2、BindingNavigatorMain的代碼如下:
private int pageSize; //每頁顯示記錄數 private int pageIndex; //頁序號 private int totalCount; //總記錄數 private int pageCount; //總頁數 public BindingNavigatorMain() { InitializeComponent(); } private void BindingNavigatorMain_Load(object sender, EventArgs e) { pageSize = 20; pageIndex = 0; SetPage(); } //設置頁 private void SetPage() { //總記錄數 totalCount = 0; BindPage(pageSize, pageIndex + 1, out totalCount); //總頁數 if (totalCount % pageSize == 0) pageCount = totalCount / pageSize; else pageCount = totalCount / pageSize + 1; //當前頁及總頁數 txtCurrentPage.Text = (pageIndex + 1).ToString(); lblTotalPage.Text = "共 " + pageCount.ToString() + " 頁"; //BindingNavigator數據源不進行BindingSource賦值,但恢復控件可用性。 bindingNavigatorMoveFirstItem.Enabled = true; bindingNavigatorMovePreviousItem.Enabled = true; txtCurrentPage.Enabled = true; lblTotalPage.Enabled = true; bindingNavigatorMoveNextItem.Enabled = true; bindingNavigatorMoveLastItem.Enabled = true; } /// <summary> /// 綁定頁 /// </summary> /// <param name="pageSize">每頁顯示記錄數</param> /// <param name="pageIndex">頁序號</param> /// <param name="totalCount">總記錄數</param> private void BindPage(int pageSize, int pageIndex, out int totalCount) { SqlConnection conn = null; SqlCommand cmd = null; totalCount = 0; #region 連接數據庫測試 try { //數據庫連接 conn = new SqlConnection("server=.;database=DB_TEST;Uid=sa;pwd=********;"); conn.Open(); //SqlCommand cmd = new SqlCommand(); cmd.Connection = conn; cmd.CommandText = "PageTest"; cmd.CommandType = CommandType.StoredProcedure; SqlParameter[] param = { new SqlParameter("@PageSize",SqlDbType.Int), new SqlParameter("@PageIndex",SqlDbType.Int), new SqlParameter("@TotalCount",SqlDbType.Int) }; param[0].Value = pageSize; param[1].Value = pageIndex; param[2].Direction = ParameterDirection.Output; cmd.Parameters.AddRange(param); //DataTable DataTable dt = new DataTable("MF_MO"); dt.Columns.Add(new DataColumn("MO_NO", typeof(String))); dt.Columns.Add(new DataColumn("MRP_NO", typeof(String))); dt.Columns.Add(new DataColumn("QTY", typeof(Decimal))); dt.Columns.Add(new DataColumn("BIL_NO", typeof(String))); #region 方法一:SqlDataReader SqlDataReader dr = cmd.ExecuteReader(); dt.Load(dr, LoadOption.PreserveChanges); dr.Close(); totalCount = (int)param[2].Value; dataGridView1.DataSource = dt; #endregion #region #方法二:SqlDataAdapter //SqlDataAdapter da = new SqlDataAdapter(); //da.SelectCommand = cmd; //dt.BeginLoadData(); //da.Fill(dt); //dt.EndLoadData(); //totalCount = (int)param[2].Value; //dataGridView1.DataSource = dt; #endregion } catch (Exception ex) { MessageBox.Show(ex.Message, "提示", MessageBoxButtons.OK, MessageBoxIcon.Information); } finally { conn.Close(); cmd.Dispose(); } #endregion } /// <summary> /// 首頁 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void bindingNavigatorMoveFirstItem_Click(object sender, EventArgs e) { pageIndex = 0; SetPage(); } /// <summary> /// 上一頁 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void bindingNavigatorMovePreviousItem_Click(object sender, EventArgs e) { pageIndex--; if (pageIndex < 0) { pageIndex = 0; } SetPage(); } /// <summary> /// 下一頁 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void bindingNavigatorMoveNextItem_Click(object sender, EventArgs e) { pageIndex++; if (pageIndex > pageCount - 1) { pageIndex = pageCount - 1; } SetPage(); } /// <summary> /// 末頁 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void bindingNavigatorMoveLastItem_Click(object sender, EventArgs e) { pageIndex = pageCount - 1; SetPage(); } /// <summary> /// 只能按0-9、Delete、Enter、Backspace鍵 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void txtCurrentPage_KeyPress(object sender, KeyPressEventArgs e) { if ((e.KeyChar >= 48 && e.KeyChar <= 57) || e.KeyChar == 8 || e.KeyChar == 13 || e.KeyChar == 127) { e.Handled = false; if (e.KeyChar == 13) { Go(); } } else { e.Handled = true; } } /// <summary> /// 指定頁 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void btnGo_Click(object sender, EventArgs e) { Go(); } private void Go() { if (string.IsNullOrEmpty(txtCurrentPage.Text)) { MessageBox.Show("指定頁不能為空。", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information); txtCurrentPage.Focus(); return; } if (int.Parse(txtCurrentPage.Text) > pageCount) { MessageBox.Show("指定頁已超過總頁數。", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information); txtCurrentPage.Focus(); return; } pageIndex = int.Parse(txtCurrentPage.Text) - 1; SetPage(); }
3、SQL Server創建存儲過程PageTest:
CREATE PROCEDURE [dbo].[PageTest] @PageSize INT, @PageIndex INT, @TotalCount INT OUTPUT AS BEGIN --總記錄數 SELECT @TotalCount=COUNT(1) FROM MF_MO --記錄返回(使用動態SQL繞開參數嗅探問題,效率大幅度提升。) DECLARE @SQL NVARCHAR(1000) SET @SQL= 'SELECT TOP ('+CONVERT(VARCHAR(32),@PageSize)+') MO_NO,MRP_NO,QTY,BIL_NO '+ 'FROM MF_MO A '+ 'WHERE NOT EXISTS (SELECT 1 FROM (SELECT TOP ('+CONVERT(VARCHAR(32),(@PageIndex-1)*@PageSize)+') MO_NO FROM MF_MO ORDER BY MO_NO) B WHERE A.MO_NO=B.MO_NO) '+ 'ORDER BY MO_NO' EXEC (@SQL) END
4、執行程序:
以上就是怎么在DataGridView中使用BindingNavigator實現一個分頁功能,小編相信有部分知識點可能是我們日常工作會見到或用到的。希望你能通過這篇文章學到更多知識。更多詳情敬請關注億速云行業資訊頻道。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。