datagridview的查询分页
/首次加载 private void button1_Click(object sender, EventArgs e)
{ //查询();
}
//定义每页多少条pagesize 要查询页码currentpage 总共多少页pagecount 总共条数datasize
int pagesize = 10; int currentpage = 1;
//int pagecount = 0;(datasize-1) / pagesize + 1)
int datasize = 0;
//查询 每页条数pagesize 要查询页码
private void query(int pagesize,int currentpage)
{ con.Open();
SqlDataAdapter sqlda = new SqlDataAdapter("select top " + pagesize + "id, 修改人,操作名称,名称编号,物品名称,规格,种类,库存底线,库存上线,初始数量,初始价格,初始金额,给车间成本价,存放位置,备注,当前存量,检验类型,产品来源,供应商编号,供应商,采购策略,品牌,计量单位,仓库,辅助单位,初始值,现存量,颜色,采购周期,具体用途,质量要求,使用部门,图片,供应商简称,主辅 from 物品名称日志表 where id not in(select top " + pagesize * (currentpage - 1) + " id from 物品名称日志表 order by id desc )order by id desc ", con);
DataSet ds = new DataSet();
sqlda.Fill(ds);
this.dataGridView1.DataSource = ds.Tables[0].DefaultView;
this.dataGridView1.Columns[0].HeaderCell.Style.ForeColor = Color.Red;
con.Close(); }
//首次加载查询
public void 查询()
{ con.Open();
//得出总条数
SqlDataAdapter sqlda = new SqlDataAdapter("select 修改人,操作名称,名称编号,物品名称,规格,种类,库存底线,库存上线,初始数量,初始价格,初始金额,给车间成本价,存放位置,备注,当前存量,检验类型,产品来源,供应商编号,供应商,采购策略,品牌,计量单位,仓库,辅助单位,初始值,现存量,颜色,采购周期,具体用途,质量要求,使用部门,图片,供应商简称,主辅 from 物品名称日志表 order by id desc ", con); DataSet ds1 = new DataSet();
sqlda.Fill(ds1);
datasize = ds1.Tables[0].Rows.Count;
con.Close();
currentpage = 1;
query(pagesize, currentpage);
this.第几页.Text = "1";
this.共几页.Text = ((datasize - 1) / pagesize + 1).ToString();
query(pagesize,currentpage);
}
private void 首页_Click(object sender, EventArgs e)
{ currentpage = 1; query(pagesize, currentpage); this.第几页.Text = "1"; }
private void 上一页_Click(object sender, EventArgs e)
{
if (int.Parse(this.第几页.Text.Trim()) == 1) { MessageBox.Show("已经是第一页了,到家了"); }
else {
currentpage = int.Parse(this.第几页.Text.Trim()) - 1; query(pagesize, currentpage); this.第几页.Text = currentpage.ToString();
}
}
private void 下一页_Click(object sender, EventArgs e)
{ if (int.Parse(this.第几页.Text.Trim()) == ((datasize - 1) / pagesize + 1))
{
MessageBox.Show("已经是最后一页了,到家了");
}
else {
currentpage = int.Parse(this.第几页.Text.Trim())+1; query(pagesize, currentpage); this.第几页.Text = currentpage.ToString();
}
}
private void 未页_Click(object sender, EventArgs e)
{ if (currentpage == ((datasize-1) / pagesize + 1))
{
MessageBox.Show("已经是最后一页了,到家了");
}
else {
currentpage = (datasize - 1) / pagesize + 1; query(pagesize, currentpage); this.第几页.Text = ((datasize - 1) / pagesize + 1).ToString();
}
}
private void 跳转_Click(object sender, EventArgs e)
{ //正整数正则 System.Text.RegularExpressions.Regex reg1 = new System.Text.RegularExpressions.Regex(@"^[0-9]\d*$");
if(reg1.IsMatch(this.跳.Text.Trim().ToString()))
{ if(int.Parse(this.跳.Text.Trim())>((datasize-1) / pagesize + 1))
{ MessageBox.Show("请输入正确的数字"); return; }else
{
//跳转到某页
currentpage = int.Parse(this.跳.Text.Trim());
query(pagesize, currentpage);
this.第几页.Text = currentpage.ToString();
}
}
else
{
MessageBox.Show("请输入正确的数字"); return;
}
}