DataGridView使用BindingNavigator实现简单分页功能

  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;

  }

  ///

  /// 绑定页

  ///

  /// 每页显示记录数

  /// 页序号

  /// 总记录数

  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

  }

  ///

  /// 首页

  ///

  ///

  ///

  private void bindingNavigatorMoveFirstItem_Click(object sender, EventArgs e)

  {

  pageIndex = 0;

  SetPage();

  }

  ///

  /// 上一页

  ///

  ///

  ///

  private void bindingNavigatorMovePreviousItem_Click(object sender, EventArgs e)

  {

  pageIndex--;

  if (pageIndex < 0)

  {

  pageIndex = 0;

  }

  SetPage();

  }

  ///

  /// 下一页

  ///

  ///

  ///

  private void bindingNavigatorMoveNextItem_Click(object sender, EventArgs e)

  {

  pageIndex++;

  if (pageIndex > pageCount - 1)

  {

  pageIndex = pageCount - 1;

  }

  SetPage();

  }

  ///

  /// 末页

  ///

  ///

  ///

  private void bindingNavigatorMoveLastItem_Click(object sender, EventArgs e)

  {

  pageIndex = pageCount - 1;

  SetPage();

  }

  ///

  /// 只能按0-9、Delete、Enter、Backspace键

  ///

  ///

  ///

  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;

  }

  }

  ///

  /// 指定页

  ///

  ///

  ///

  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();

  }