public void fenye()
{
//string connstring = commDBHelper.connDb();
//MySqlConnection conn = new MySqlConnection(connstring);
//conn.Open();//打开数据库,连接
string mysql = "select count(*) from Hospital ";
// SqlCommand cmd = new SqlCommand(mysql, conn);
DataTable dt=sqlcommDBHelper.ExecuteReader(mysql,CommandType.Text,null);
int sum = Convert.ToInt32(dt.Rows[0][0].ToString ());//记录总数
Label3.Text = sum.ToString();
string mysql1 = "select * from Hospital ";
//MySqlDataAdapter myda = new MySqlDataAdapter(mysql1, conn);
DataSet ds = new DataSet();
ds = sqlcommDBHelper.ExecuteDataSetReader(mysql1, CommandType.Text, null);
PagedDataSource pds = new PagedDataSource();
pds.DataSource = ds.Tables[0].DefaultView;
pds.AllowPaging = true;//允许分页
pds.PageSize = 6;
pds.CurrentPageIndex = Convert.ToInt32(Label1.Text) - 1;//计算当前显示的页数
contextDataGrid.DataSource = pds;
Label2.Text = pds.PageCount.ToString();//显示总共多少页
Label1.Text = (pds.CurrentPageIndex + 1).ToString();//显示当前页数
this.LinkButton1.Enabled = true;
this.LinkButton2.Enabled = true;
this.LinkButton3.Enabled = true;
this.LinkButton4.Enabled = true;
if (pds.CurrentPageIndex < 1)
{
this.LinkButton1.Enabled = false;
this.LinkButton2.Enabled = false;
}
if (pds.CurrentPageIndex == pds.PageCount - 1)
{
this.LinkButton3.Enabled = false;
this.LinkButton4.Enabled = false;
}
contextDataGrid.DataBind();
}
protected void LinkButton1_Click(object sender, EventArgs e)
{
Label1.Text = "1";
fenye();
}
protected void LinkButton2_Click(object sender, EventArgs e)
{
Label1.Text = Convert.ToString(Convert.ToInt32(Label1.Text) - 1);
fenye();
}
protected void LinkButton3_Click(object sender, EventArgs e)
{
Label1.Text = Convert.ToString(Convert.ToInt32(Label1.Text) + 1);
fenye();
}
protected void LinkButton4_Click(object sender, EventArgs e)
{
Label1.Text = Label2.Text;
fenye();
}
private void DataListBind()
{
PagedDataSource pds = new PagedDataSource();
pds.AllowPaging = true; //实现分页功能
pds.CurrentPageIndex = CurrentPage; //使用状态保存当前页数
pds.DataSource = zd_unit_codeManagers.selectAllZd_unit_code();
pds.PageSize = 3; //每页显示数据条数
PageCount = pds.PageCount; //总共数据数量
this.Label1.Text = "第" + CurrentPage + "页,共" + (PageCount -1) + "页";
this.GridView1.DataSource = pds;
this.GridView1.DataBind();
buttonShowAndEnabled();
}
/// <summary>
/// 按钮可用不可用
/// </summary>
public void buttonShowAndEnabled()
{
if (CurrentPage <= 1)
{
this.btnUpPage.Enabled = false;
}
else
{
this.btnUpPage.Enabled = true;
}
if (CurrentPage >= PageCount - 1)
{
this.btnDownPage.Enabled = false;
}
else
{
this.btnDownPage.Enabled = true;
}
}
protected void btnUpPage_Click(object sender, EventArgs e)
{
CurrentPage--;
DataListBind();
}
protected void btnDownPage_Click(object sender, EventArgs e)
{
CurrentPage++;
DataListBind();
}
protected void btnUpPage_Click(object sender, EventArgs e)
{
CurrentPage--;
DataListBind();
}
protected void btnDownPage_Click(object sender, EventArgs e)
{
CurrentPage++;
DataListBind();
}
你可以看下 EasyCode .NEt 代码生成器 生成出来的界面有分页功能的,有源码的~~http://www.cnblogs.com/BudEasyCode/archive/2012/02/27/2370549.html#2327705