打算要实现的效果是:

由于listview控件本身没有分页的功能,为了达到这么目的,

方法一:我们可以结合其他空间来完成,但是对framework要求的版本也比较高;

方法二:通过sql语句来完成,通过sql语句完成分页。

这里我们采用第二种方法:

namespace seperatePage
{
    public partial class Form1 : Form
    {
        string sql;
        SqlConnectionStringBuilder bldr;
        SqlConnection cn;      
        int totalcount = 0;//记录总共的记录数
        static int page = 0;   //记录现在翻到第几页了  
        public Form1()
        {
            InitializeComponent();
            linkDatabase();
        }
        public void linkDatabase()
        {//连接数据库,打开连接
            bldr = new SqlConnectionStringBuilder();
            bldr.DataSource = “222.195.150.228”;
            bldr.InitialCatalog = “shortmessage”;
            bldr.IntegratedSecurity = false;
            bldr.UserID = “shortmessage”;
            bldr.Password = “shortmessageuser”;
            cn = new SqlConnection(bldr.ConnectionString);
            cn.Open();

        }
        public void closeDatabase()
        {
            //关闭连接
            cn.Close();
        }
        public DataSet selectData(String sql)
        {
            //查询数据集
            DataSet ds = new DataSet();
            SqlDataAdapter da = new SqlDataAdapter(sql, cn);
            SqlCommandBuilder cb = new SqlCommandBuilder(da);
            da.Fill(ds);
            return ds;

        }
        private void Form1_Load(object sender, EventArgs e)
        {

//窗体加载的时候,前5条记录是显示的。这时候,“下一页”是可用的,“上一页”是不可用的。
            nextPage.Enabled = true ;
            prevPage.Enabled = false;
            listView1.GridLines = false ;           
            listView1.Columns.Add(“name”);
            listView1.Columns.Add(“age”);
            listView1.Columns.Add(“rollno”);
            listView1.View = View.Details;           
            listView1.Items.Clear();
            sql = “select * from test”;
            DataSet ds_one = selectData(sql);
            DataTable dt_one = ds_one.Tables[0];
            for (int i = 0; i < dt_one.Rows.Count; i++)
            {
                totalcount = totalcount + 1;//查看一共有多少条记录。
            }        
            sql = “select top 5 name,age,rollno from test “;  //显示前5条信息        
            DataSet ds = selectData(sql);
            write_listview(ds); //这个函数的作用是用来往listview中写数据的。        
         
        }

        private void next_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {

//当点击下一页时,上一页是可用的。
           prevPage.Enabled  = true;

//记录有可能是被5整除的,有可能是不能被5整除,所以判断下一页是否可用需要分情况
           if (totalcount % 5 == 0)
           {
               if (page >= totalcount / 5 – 2)
               {
                   nextPage.Enabled = false;
                   prevPage.Enabled = true;
               }
           }
           else
           {
               if (page >= totalcount / 5 – 1)
               {
                   nextPage.Enabled = false;
                   prevPage.Enabled = true;
               }
           }
           page = page + 1;
            listView1.Items.Clear();
            sql = “select top 5 name,age,rollno from test where rollno not in (select top “+page*5+” rollno from test)”;           
            DataSet ds = selectData(sql);
            write_listview(ds);           
        }

        private void linkLabel2_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            nextPage.Enabled = true;
            page = page – 1;
            if (page <= 0)
            {
                nextPage.Enabled = true ;
                prevPage.Enabled = false ;
            }
            listView1.Items.Clear();
            sql = “select top 5 name,age,rollno from test where rollno not in (select top ” + page * 5 + ” rollno from test)”;
          
            DataSet ds = selectData(sql);
             write_listview(ds);           
        }

        private void firstPage_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            listView1.Items.Clear();
            prevPage.Enabled = false;
            nextPage.Enabled = true;
            sql = “select top 5 name,age,rollno from test “;         
            page = 0;//必须要写,否则page会出现问题。
            DataSet ds = selectData(sql);
            write_listview(ds);          
        }
        private void write_listview(DataSet ds)
        {//用于完成向listview添加信息。
            ListViewItem l = null;
            DataTable dt = ds.Tables[0];
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                l = new ListViewItem(dt.Rows[i][“name”].ToString());
                l.SubItems.Add(dt.Rows[i][“age”].ToString());
                l.SubItems.Add(dt.Rows[i][“rollno”].ToString());
                listView1.Items.Add(l);
            }
        }
        private void lagePage_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            if (totalcount % 5 == 0)
            {
                page = totalcount / 5 – 1;
            }
            else
            {
                page = totalcount / 5;
            }
            nextPage.Enabled = false;
            prevPage.Enabled = true;
            listView1.Items.Clear();
            sql = “select top 5 name,age,rollno from test where rollno not in (select top ” + page * 5 + ” rollno from test)”;          
            DataSet ds = selectData(sql);
            write_listview(ds);          
        }
    }
}

版权声明:本文为bingoyan原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://www.cnblogs.com/bingoyan/archive/2012/10/23/2735946.html