winform ListView和DataGridView实现分页

Posted zhenzhen78

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了winform ListView和DataGridView实现分页相关的知识,希望对你有一定的参考价值。

 

用到的控件有

 

code:

listviewPaging

复制代码
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace DataGridViewPage1
{
    public partial class ListViewPaging : Form
    {
        int pageSize = 0;     //每页显示行数
        int nMax = 0;         //总记录数
        int pageCount = 0;    //页数=总记录数/每页显示行数
        int pageCurrent = 0;   //当前页号
        int nCurrent = 0;      //当前记录行
        DataSet ds = new DataSet();
        DataTable dtInfo = new DataTable();

        public ListViewPaging()
        {
            InitializeComponent();
            BindData();
        }

        void BindData()
        {

            string strConn = "SERVER=.;DATABASE=WL_JLB;UID=sa;PWD=123456";   //数据库连接字符串
            SqlConnection conn = new SqlConnection(strConn);
            conn.Open();
            string strSql = "SELECT * FROM dbo.WlcSys_Model";
            SqlDataAdapter sda = new SqlDataAdapter(strSql, conn);
            sda.Fill(ds, "ds");
            conn.Close();
            dtInfo = ds.Tables[0];
            InitDataSet();

        }

        private void InitDataSet()
        {
            pageSize = 20;      //设置页面行数
            nMax = dtInfo.Rows.Count;

            pageCount = (nMax / pageSize);    //计算出总页数

            if ((nMax % pageSize) > 0) pageCount++;

            pageCurrent = 1;    //当前页数从1开始
            nCurrent = 0;       //当前记录数从0开始

            LoadData();
        }

        private void LoadData()
        {
            int nStartPos = 0;   //当前页面开始记录行
            int nEndPos = 0;     //当前页面结束记录行

            DataTable dtTemp = dtInfo.Clone();   //克隆DataTable结构框架

            if (pageCurrent == pageCount)
                nEndPos = nMax;
            else
                nEndPos = pageSize * pageCurrent;

            nStartPos = nCurrent;

            //lblPageCount2.Text = pageCount.ToString();
            txtCurrentPage.Text = Convert.ToString(pageCurrent);
            lblPageCount.Text = pageCount.ToString();

            this.listView1.Items.Clear();
            //从元数据源复制记录行
            for (int i = nStartPos; i < nEndPos; i++)
            {
                dtTemp.ImportRow(dtInfo.Rows[i]);

                //dtInfo.Rows[i].ItemArray[0]
                //14
                //dtInfo.Rows[i].ItemArray[2]
                //"在线对应参数设置"

                var lvi = new ListViewItem(new string[] { dtInfo.Rows[i].ItemArray[0].ToString(),
                    dtInfo.Rows[i].ItemArray[2].ToString()
                        });

                listView1.Items.Add(lvi);
                nCurrent++;
            }
            bdsInfo.DataSource = dtTemp;
            bdnInfo.BindingSource = bdsInfo;
            //listView1.DataSource = bdsInfo;
        }

        private void bdnInfo_ItemClicked(object sender, ToolStripItemClickedEventArgs e)
        {
            if (e.ClickedItem.Text == "关闭")
            {
                this.Close();
            }
            if (e.ClickedItem.Text == "上一页")
            {
                pageCurrent--;
                if (pageCurrent <= 0)
                {
                    MessageBox.Show("已经是第一页,请点击“下一页”查看!");
                    pageCurrent++;
                    return;
                }
                else
                {
                    nCurrent = pageSize * (pageCurrent - 1);
                }

                LoadData();
            }
            if (e.ClickedItem.Text == "下一页")
            {
                pageCurrent++;
                if (pageCurrent > pageCount)
                {
                    MessageBox.Show("已经是最后一页,请点击“上一页”查看!");
                    pageCurrent--;
                    return;
                }
                else
                {
                    nCurrent = pageSize * (pageCurrent - 1);
                }
                LoadData();
            }
            if (e.ClickedItem.Text == "go")
            {
                int a = 0;
                int.TryParse(txtCurrentPage.Text, out a);
                if (a == 0)
                {
                    MessageBox.Show("只能输入正整数!");
                    return;
                }
                //pageCurrent = int.Parse(txtCurrentPage.Text);
                pageCurrent = a;
                if (pageCurrent <= 0)
                {
                    MessageBox.Show("已经是第一页,请点击“下一页”查看!");
                    return;
                }
                if (pageCurrent > pageCount)
                {
                    MessageBox.Show("已经是最后一页,请点击“上一页”查看!");
                    return;
                }
                nCurrent = pageSize * (pageCurrent - 1);
                LoadData();
            }
        }
    }
}
复制代码

 

datagridviewpaging

复制代码
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace DataGridViewPage1
{
    public partial class Form1 : Form
    {
        int pageSize = 0;     //每页显示行数
        int nMax = 0;         //总记录数
        int pageCount = 0;    //页数=总记录数/每页显示行数
        int pageCurrent = 0;   //当前页号
        int nCurrent = 0;      //当前记录行
        DataSet ds = new DataSet();
        DataTable dtInfo = new DataTable();

        public Form1()
        {
            InitializeComponent();
            BindData();
        }


        void BindData()
        {

            string strConn = "SERVER=.;DATABASE=WL_JLB;UID=sa;PWD=123456";   //数据库连接字符串
            SqlConnection conn = new SqlConnection(strConn);
            conn.Open();
            string strSql = "SELECT * FROM dbo.WlcSys_Model";
            SqlDataAdapter sda = new SqlDataAdapter(strSql, conn);
            sda.Fill(ds, "ds");
            conn.Close();
            dtInfo = ds.Tables[0];
            InitDataSet();

        }

        private void InitDataSet()
        {
            pageSize = 20;      //设置页面行数
            nMax = dtInfo.Rows.Count;

            pageCount = (nMax / pageSize);    //计算出总页数

            if ((nMax % pageSize) > 0) pageCount++;

            pageCurrent = 1;    //当前页数从1开始
            nCurrent = 0;       //当前记录数从0开始

            LoadData();
        }

        private void LoadData()
        {
            int nStartPos = 0;   //当前页面开始记录行
            int nEndPos = 0;     //当前页面结束记录行

            DataTable dtTemp = dtInfo.Clone();   //克隆DataTable结构框架

            if (pageCurrent == pageCount)
                nEndPos = nMax;
            else
                nEndPos = pageSize * pageCurrent;

            nStartPos = nCurrent;

            //lblPageCount2.Text = pageCount.ToString();
            txtCurrentPage.Text = Convert.ToString(pageCurrent);
            lblPageCount.Text = pageCount.ToString();

            //从元数据源复制记录行
            for (int i = nStartPos; i < nEndPos; i++)
            {
                dtTemp.ImportRow(dtInfo.Rows[i]);
                nCurrent++;
            }
            bdsInfo.DataSource = dtTemp;
            bdnInfo.BindingSource = bdsInfo;
            dgvInfo.DataSource = bdsInfo;
        }

        private void bdnInfo_ItemClicked(object sender, ToolStripItemClickedEventArgs e)
        {
            if (e.ClickedItem.Text == "关闭")
            {
                this.Close();
            }
            if (e.ClickedItem.Text == "上一页")
            {
                pageCurrent--;
                if (pageCurrent <= 0)
                {
                    MessageBox.Show("已经是第一页,请点击“下一页”查看!");
                    pageCurrent++;
                    return;
                }
                else
                {
                    nCurrent = pageSize * (pageCurrent - 1);
                }

                LoadData();
            }
            if (e.ClickedItem.Text == "下一页")
            {
                pageCurrent++;
                if (pageCurrent > pageCount)
                {
                    MessageBox.Show("已经是最后一页,请点击“上一页”查看!");
                    pageCurrent--;
                    return;
                }
                else
                {
                    nCurrent = pageSize * (pageCurrent - 1);
                }
                LoadData();
            }
            if (e.ClickedItem.Text == "go")
            {
               int a=0;
               int.TryParse(txtCurrentPage.Text, out a);
               if (a == 0)
               {
                   MessageBox.Show("只能输入正整数!");
                   return;  
               }
                //pageCurrent = int.Parse(txtCurrentPage.Text);
                pageCurrent = a;
                if (pageCurrent <= 0)
                {
                    MessageBox.Show("已经是第一页,请点击“下一页”查看!");
                    return;
                }
                if (pageCurrent > pageCount)
                {
                    MessageBox.Show("已经是最后一页,请点击“上一页”查看!");
                    return;
                }
                nCurrent = pageSize * (pageCurrent - 1);
                LoadData();
            }
        }
    }
}
复制代码

以上是关于winform ListView和DataGridView实现分页的主要内容,如果未能解决你的问题,请参考以下文章

winForm如何实现ListView控件中数据的换行显示?

C# .net不同版本中winform控件 dataGrid /DataGrid/DataGridView 有啥区别

listview在上gridview在下,如何能够一起滚动?

WPF入门教程系列二十一——DataGrid示例

datagrid数据源绑定winform和wpf的区别

请问一下listview如何向gallery那样滚动