zoukankan      html  css  js  c++  java
  • 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();
                }
            }
        }
    }

  • 相关阅读:
    概率论基础
    感知机
    CSS3实现jquery的特效
    有品质的生活
    table点击一行显示下一行的特效
    colspan在浏览器中失效的问题
    css的框架——common.css
    使用 document.onreadystatechange()来判断页面加载完
    iframe中子页面通过js计算高度(使得页面不会显示不全)
    js返回上一页并刷新的多种方法
  • 原文地址:https://www.cnblogs.com/wenjie0904/p/9903842.html
Copyright © 2011-2022 走看看