目前只能实现单个记录的导航,但是要实现分页如何做呢?
请给个思路 谢谢!
[CODE]int pagecount=0;
int recNo=0;
int currentpage=1;
int pagesize=12;
int maxrec=0;
DataSet ds;
DataTable dt;
private void Form1_Load(object sender, System.EventArgs e)
{
SqlConnection conn=new SqlConnection("Server=purana;Database=Northwind;Integrated Security=SSPI");
SqlCommand cmd=new SqlCommand();
cmd.CommandText="Select * From Orders";
cmd.Connection=conn;
SqlDataAdapter ada=new SqlDataAdapter();
ada.SelectCommand=cmd;
ds=new DataSet();
conn.Open();
ada.Fill(ds,"Orders");
conn.Close();
dt=ds.Tables["Orders"];
maxrec=dt.Rows.Count;
pagecount=maxrec/pagesize;
if(maxrec%pagesize>0)
{
pagecount++;
}
recNo=0;
currentpage=1;
loaddata();
}
private void loaddata()
{
DataTable tempdt;
int Startrec;
int Endrec;
tempdt=dt.Clone();
if (currentpage==pagecount)
{
Endrec=maxrec;
}
else
{
Endrec=pagesize*currentpage;
}
Startrec=recNo;
int i=0;
for(i=Startrec;i<Endrec;i++)
{
tempdt.ImportRow(dt.Rows[i]);
recNo++;
}
dataGrid1.DataSource=tempdt;
pageinfo();
}
private void pageinfo()
{
textBox1.Text=currentpage.ToString() + "/" +pagecount.ToString();
}
private void button2_Click(object sender, System.EventArgs e)
{
currentpage--;
if(currentpage<1)
{
currentpage=1;
return;
}
else
{
recNo=pagesize*(currentpage-1);
}
loaddata();
}
private void button1_Click(object sender, System.EventArgs e)
{
if(currentpage!=1)
{
currentpage=1;
recNo=0;
loaddata();
}
}
private void button3_Click(object sender, System.EventArgs e)
{
currentpage++;
if( currentpage>pagecount)
{
currentpage=pagecount;
if (recNo==maxrec)
{
return;
}
}
loaddata();
}
private void button4_Click(object sender, System.EventArgs e)
{
if (maxrec==recNo)
{
return;
}
currentpage=pagecount;
recNo=pagesize*(currentpage-1);
loaddata();
}[/CODE]