dgv分页
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
#region 分页显示 static int rowsall = 0;//总行数 static int pageall = 0;//总页数 static int page = 0;//第几页 static int count =20;//返回5行 static int start = 0;//从第start行开始返回 public string selectsql, tablename; public void upPageX(string sqlstr, string table, DataGridView dgv, DevComponents.DotNetBar.ButtonX btn1, DevComponents.DotNetBar.ButtonX btn2, DevComponents.DotNetBar.Controls.ComboBoxEx cmb) { rowsall = getds(sqlstr, table).Tables[table].Rows.Count;//总行数 if (rowsall == 0) { //如果没有数据则将第一页、上一页、下一页、最后一页设置为不可用;并设置其他参数 page = 0; pageall = 0; rowsall = 0; dgv.DataSource = gettb(sqlstr, start, count, table);//绑定DataGridView cmb.Items.Clear(); return; } if (rowsall > 0)//判断是否有内容 { page = 1;//如果有内容,设置为第一页 start = 0; } int yushu = rowsall % count;//是否存在余行 if (yushu == 0)//不存在余行时设置总页数 { if (rowsall > 0 && rowsall <= count) { pageall = 1; } else { pageall = rowsall / count; } } else//存在余行时设置总页数 { pageall = rowsall / count + 1; } { if (pageall > 0) { //设置跳转到第几页 cmb.Items.Clear(); for (int i = 1; i <= pageall; i++) { cmb.Items.Add(i); } } } selectsql = sqlstr; tablename = table; dgv.DataSource = gettb(selectsql, start, count, table);//绑定DataGridView } public DataTable gettb(string sql, int start, int count, string tablename) { SqlConnection con = SQLHelper.CreateConnection(); con.Open(); DataSet myds = new DataSet(); SqlDataAdapter sda = new SqlDataAdapter(sql, con); sda.Fill(myds, start, count, tablename); return myds.Tables[tablename]; } public DataSet getds(string M_str_sqlstr,string table)//得到绑定的所有的数据集 { DataSet ds = new DataSet(); ds=SQLHelper.ExecuteDataSet(M_str_sqlstr,table,null); return ds; } #endregion private void paging() { //upPage("select * from t_News", "t_News",dgvSenderMess ,btnUp1 ,btnDown1 ,cmbSel1 ); upPageX("select * from t_SendMessage", "t_SendMessage", dgvSenderMess, btnUp1, btnDown1, cmbSel1); label13.Text = "共" + rowsall.ToString() + "条记录"; if (cmbSel1.Items.Count > 0) cmbSel1.SelectedIndex = 0; } private void SendLog() { upPageX("select * from t_SendLog", "t_SendLog", dgvInfo, btnUp5, btnDown5, cmbSel5); label1.Text = "共"+rowsall.ToString ()+"条记录"; if (cmbSel5.Items.Count > 0) cmbSel5.SelectedIndex = 0; }
添加2button控件,一个cmb控件
private void btnUp6_Click(object sender, EventArgs e) { if (cmbSel6.Text != "") { if (Convert.ToInt32(cmbSel6.Text) > 1) { cmbSel6.SelectedIndex = Convert.ToInt32(cmbSel6.Text) - 2; } else if (Convert.ToInt32(cmbSel6.Text) == 1) { cmbSel6.SelectedIndex = 0; } } } private void btnDown6_Click(object sender, EventArgs e) { if (cmbSel6.Text != "") { if (Convert.ToInt32(cmbSel6.Text) < cmbSel6.Items.Count) { cmbSel6.SelectedIndex = Convert.ToInt32(cmbSel6.Text); } } } private void cmbSel6_SelectedIndexChanged(object sender, EventArgs e) { page = Convert.ToInt32(cmbSel6.Text.Trim()); start = (page - 1) * count; this.dgvPeople.DataSource = gettb(selectsql, start, count, tablename);//绑定DataGridView }
调用只需要 upPageX("select * from t_Grou", "t_Grou", dgvEditMess, btnUp4, btnDown4, cmbSel4);