using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.SqlClient;
namespace Pagination
{
public partial class Form1 : Form
{
static int TotalRows;
//private int CurrentRows = 0;
private int Pages = 0;
//SqlConnection connection = new SqlConnection("server=.;database=db_09;pwd=sa;uid=test");
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
using (SqlConnection connection = new SqlConnection("server=.;database=db_09;pwd=test;uid=sa"))
{
connection.Open();
SqlDataAdapter adp = new SqlDataAdapter("select * from 工资表", connection);
DataTable dt = new DataTable();
adp.Fill(dt);
TotalRows = dt.Rows.Count;
Pages = TotalRows % 8;
if (Pages == 0)
{
Pages = TotalRows / 8;
}
else
{
Pages = TotalRows / 8 + 1;
}
LblTotalPage.Text = Pages.ToString();
lblCurrentPage.Text = "1";
//CurrentRows = 8;
connection.Close();
ShowData(0, 7);
}
}
private void ShowData(int i, int j)
{
SqlConnection connection = new SqlConnection("server=.;database=db_09;pwd=test;uid=sa");
using (SqlCommand command = new SqlCommand("select * from 工资表", connection))
{
//connection.Open();
DataSet ds = new DataSet();
SqlDataAdapter adp = new SqlDataAdapter(command);
adp.Fill(ds, i, j, "工资表");
this.dataGridView1.DataSource = ds.Tables["工资表"].DefaultView;
connection.Close();
}
}
//首页
private void lklblFirst_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
{
ShowData(0, 8);
lblCurrentPage.Text = "1";
; }
//末尾页
private void lklblEnd_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
{
ShowData((Pages - 1) * 8, 8);
lblCurrentPage.Text = Pages.ToString();
}
//上一页
private void lklblPreview_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
{
if (lblCurrentPage.Text == "1")
{
MessageBox.Show("当前已经为首页!");
}
else
{
ShowData(((Convert.ToInt16(lblCurrentPage.Text)) - 1) * 8, 8);
lblCurrentPage.Text = (Convert.ToInt16(lblCurrentPage.Text) - 1).ToString();
}
}
//下一页
private void lklblNext_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
{
if (lblCurrentPage.Text == Pages.ToString())
{
MessageBox.Show("当前页面已经为最后一页了!");
}
else
{
ShowData((Convert.ToInt16(lblCurrentPage.Text)) * 8, 8);
lblCurrentPage.Text = (Convert.ToInt16(lblCurrentPage.Text) + 1).ToString();
}
}
}
}
c#-winform中DataGridView的数据实现分页