Datagridview 分页显示数据

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();
            }
        }
    }
}

转载于:https://www.cnblogs.com/wenjie0904/p/9903842.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值