zoukankan      html  css  js  c++  java
  • c#-winform中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();
                }
            }
        }
    }
  • 相关阅读:
    Spring框架之 我对AOP的理解
    第二次分班考试之 ---纠错19/25题
    Spring IOC(控制反转) 和DI
    一级缓存,二级缓存
    多对多连接
    MyBatis 智能标签
    小结javaScriptOOP的对象内容点
    15年错题小结2月
    《Java周边》Http请求模拟工具(postman)
    《Java周边》IDEA 设置快捷键和快捷键中英文对照
  • 原文地址:https://www.cnblogs.com/kcwang/p/14966516.html
Copyright © 2011-2022 走看看