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();
                }
            }
        }
    }
  • 相关阅读:
    SWTDesigner注册器
    C# 创建、部署和调用WebService的简单示例
    (android实战)应用在线版本更新
    jQuery获取Select选择的Text和 Value(转)
    Android 判断sd卡和sim卡是否可用
    Android开发中如何固定屏幕显示!
    入侵网站简单方法总结
    【Android】防止UI界面被输入法遮挡(画面随输入法自适应)
    关于字符编码的问题
    最好用的mysql密码忘记的解决方法
  • 原文地址:https://www.cnblogs.com/kcwang/p/14966516.html
Copyright © 2011-2022 走看看