C# winform listview分页(sql分页)

自己正在做邮件系统遇到了的listview分页功能的问题,来和大家分享下,话不多说,直接贴代码

下面是界面代码,

namespace Erp.PDM.MulMail {     partial class TestPage     {         ///

        /// Required designer variable.         ///         private System.ComponentModel.IContainer components = null;         ///         /// Clean up any resources being used.         ///         /// true if managed resources should be disposed; otherwise, false.         protected override void Dispose(bool disposing)         {             if (disposing && (components != null))             {                 components.Dispose();             }             base.Dispose(disposing);         }         #region Windows Form Designer generated code         ///         /// Required method for Designer support - do not modify         /// the contents of this method with the code editor.         ///         private void InitializeComponent()         {             this.btnPrevPage = new System.Windows.Forms.Button();             this.btnNextPage = new System.Windows.Forms.Button();             this.listEmailUser = new System.Windows.Forms.ListView();             this.columnHeader1 = ((System.Windows.Forms.ColumnHeader)(new System.Windows.Forms.ColumnHeader()));             this.columnHeader2 = ((System.Windows.Forms.ColumnHeader)(new System.Windows.Forms.ColumnHeader()));             this.SuspendLayout();             //              // btnPrevPage             //              this.btnPrevPage.Location = new System.Drawing.Point(98, 281);             this.btnPrevPage.Name = "btnPrevPage";             this.btnPrevPage.Size = new System.Drawing.Size(75, 23);             this.btnPrevPage.TabIndex = 1;             this.btnPrevPage.Text = "上一页";             this.btnPrevPage.UseVisualStyleBackColor = true;             this.btnPrevPage.Click += new System.EventHandler(this.btnPrevPage_Click);             //              // btnNextPage             //              this.btnNextPage.Location = new System.Drawing.Point(208, 280);             this.btnNextPage.Name = "btnNextPage";             this.btnNextPage.Size = new System.Drawing.Size(75, 23);             this.btnNextPage.TabIndex = 2;             this.btnNextPage.Text = "下一页";             this.btnNextPage.UseVisualStyleBackColor = true;             this.btnNextPage.Click += new System.EventHandler(this.btnNextPage_Click);             //              // listEmailUser             //              this.listEmailUser.BackColor = System.Drawing.Color.White;             this.listEmailUser.BorderStyle = System.Windows.Forms.BorderStyle.None;             this.listEmailUser.Columns.AddRange(new System.Windows.Forms.ColumnHeader[] {             this.columnHeader1,             this.columnHeader2});             this.listEmailUser.Font = new System.Drawing.Font("宋体", 10F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(134)));             this.listEmailUser.Location = new System.Drawing.Point(98, 43);             this.listEmailUser.Name = "listEmailUser";             this.listEmailUser.Size = new System.Drawing.Size(185, 277);             this.listEmailUser.TabIndex = 8;             this.listEmailUser.UseCompatibleStateImageBehavior = false;             this.listEmailUser.View = System.Windows.Forms.View.Details;             //              // columnHeader1             //              this.columnHeader1.Text = "客户、同事名称或Email";             this.columnHeader1.Width = 260;             //              // columnHeader2             //              this.columnHeader2.Text = "id";             this.columnHeader2.Width = 50;             //              // TestPage             //              this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);             this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;             this.ClientSize = new System.Drawing.Size(421, 553);             this.Controls.Add(this.btnNextPage);             this.Controls.Add(this.btnPrevPage);             this.Controls.Add(this.listEmailUser);             this.Name = "TestPage";             this.Text = "TestPage";             this.Load += new System.EventHandler(this.TestPage_Load);             this.ResumeLayout(false);         }         #endregion         private System.Windows.Forms.Button btnPrevPage;         private System.Windows.Forms.Button btnNextPage;         private System.Windows.Forms.ListView listEmailUser;         private System.Windows.Forms.ColumnHeader columnHeader1;         private System.Windows.Forms.ColumnHeader columnHeader2;     } }

以下是后台代码,简单易懂,sql句的话自己改成自己的就好了
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using Erp.Library.Data.SqlClient;
using System.Text.RegularExpressions;



namespace Erp.PDM.MulMail
{
    public partial class TestPage : Form
    {
       string sql;
       int totalcount = 0;//记录总共的记录数
       static int page =0;   //记录现在翻到第几页了  
       public TestPage()
       {
           InitializeComponent();
       }

       private ListViewItem lvi = null;

       private void TestPage_Load(object sender, EventArgs e)
       {
           //窗体加载的时候,前5条记录是显示的。这时候,“下一页”是可用的,“上一页”是不可用的。
           string status = "3";
           int count1 = GetCount(status);
           if (count1 > 50)
           {
               btnNextPage.Enabled = true;
               btnPrevPage.Enabled = false;
           }
           else
           {
               btnNextPage.Enabled = false;
               btnNextPage.Enabled = false;
           }
           string count = "";
           using (DataAdapter da = new DataAdapter())
           {
               DataTable dt = new DataTable();
               sql = "SELECT top 50 linkman,customerno,[address],FAX,POSTCODE,COUNTRYID,PHONE,shortname,CompanyName , " +
                                  "(select name from  bas_customerstyle where id=style) as style," +
                                  "(select name from  bas_customerstatus where id=CustomerStatus ) as CustomerStatus," +
                                  "(select cnname  from bas_region where id=Regionid ) as Region," +
                                  "(select cnname from bas_country where id=Countryid ) as Country," +
                                  "(select cnname from bas_province where id=Provinceid ) as Province," +
                                  "(select name   from bas_customercredit where ID=CreditRateid) as CreditRate," +
                                  "(select name  from bas_sysstatus  where ID=sysstatus ) as sysstatus ," +
                                  "dbo.GetEmailAddress(a.id) as emailaddress ,(select COUNT(*) as ecount from E_EmailInfo where E_SendId =CAST(a.customerno as varchar(50))  and (E_ReplyId = '" + Erp.PDM.User.My.UID + "' or E_CCId = '" + Erp.PDM.User.My.UID + "') and IsRead = 'false' and SUBSTRING(E_EmailInfo.E_SendId,1,1) = 'C') as ecount" +
                                         " FROM crm_customer a where 1=1 and CustomerStatus ='" + status + "'" +
                                         " ORDER BY customerno";  //显示前50条信息        
               da.Text = sql;
               da.Fill(dt);
               for (int x = 0; x < dt.Rows.Count; x++)
               {
                   count = dt.Rows[x][17].ToString();
                   if (count == "0")
                       count = "";
                   else
                       count = "(" + count + ")";
                   string name = dt.Rows[x][0].ToString() + count;// + dt.Rows[x][16].ToString()
                   string emailAddress = dt.Rows[x][16].ToString();
                   if (name == "")
                   {
                       string[] emailList = Regex.Split(emailAddress, ",", RegexOptions.IgnoreCase);
                       string email = emailList[0];
                       lvi = new ListViewItem(email);
                   }
                   else
                       lvi = new ListViewItem(name);
                   for (int k = 1; k < dt.Columns.Count; k++)
                   {
                       lvi.SubItems.Add(dt.Rows[x][k].ToString());
                   }
                   this.listEmailUser.Items.Add(lvi);
               }
               this.listEmailUser.Items.Add("");
               this.listEmailUser.Items.Add("");
           }
       }

        /// 
        /// 获取客户数量
        /// 
       /// 潜在客户、新客户、老客户
        /// 
       private int GetCount(string status)
       {
           using (DataAdapter da = new DataAdapter())
           {
               DataTable dt = new DataTable();
               sql = "select count(customerno) from crm_customer where 1=1 and CustomerStatus ='" + status + "' ";
               da.Text = sql;
               da.Fill(dt);
               //查看一共有多少条记录。
               totalcount = Convert.ToInt32(dt.Rows[0][0].ToString());
           }
           return totalcount;
       }


       private void btnPrevPage_Click(object sender, EventArgs e)
       {
           //this.listEmailUser.Clear();
           string status = "3";
           //当点击下一页时,上一页是可用的。
           //btnPrevPage.Enabled = true;
           //记录有可能是被50整除的,有可能是不能被50整除,所以判断下一页是否可用需要分情况
           if (totalcount % 50 == 0)
           {
               if (page >= totalcount / 50 - 2)
               {
                   btnNextPage.Enabled = false;
                   btnPrevPage.Enabled = true;
               }
           }
           else
           {
               if (page >= totalcount / 50 - 1)
               {
                   btnNextPage.Enabled = false;
                   btnPrevPage.Enabled = true;
               }
           }
           page = page - 1;//上一页
           if (page <= 0)
           {
               btnNextPage.Enabled = true;
               btnPrevPage.Enabled = false;
           }
           listEmailUser.Items.Clear();//清空上一页的数据加载下一页的数据
           string count = "";
           using (DataAdapter da = new DataAdapter())
           {
               DataTable dt = new DataTable();
               sql = "SELECT top 50 linkman,customerno,[address],FAX,POSTCODE,COUNTRYID,PHONE,shortname,CompanyName , " +
                                  "(select name from  bas_customerstyle where id=style) as style," +
                                  "(select name from  bas_customerstatus where id=CustomerStatus ) as CustomerStatus," +
                                  "(select cnname  from bas_region where id=Regionid ) as Region," +
                                  "(select cnname from bas_country where id=Countryid ) as Country," +
                                  "(select cnname from bas_province where id=Provinceid ) as Province," +
                                  "(select name   from bas_customercredit where ID=CreditRateid) as CreditRate," +
                                  "(select name  from bas_sysstatus  where ID=sysstatus ) as sysstatus ," +
                                  "dbo.GetEmailAddress(a.id) as emailaddress ,(select COUNT(*) as ecount from E_EmailInfo where E_SendId =CAST(a.customerno as varchar(50))  and (E_ReplyId = '" + Erp.PDM.User.My.UID + "' or E_CCId = '" + Erp.PDM.User.My.UID + "') and IsRead = 'false' and SUBSTRING(E_EmailInfo.E_SendId,1,1) = 'C') as ecount" +
                                         " FROM crm_customer a where 1=1 and CustomerStatus ='" + status + "'" +
                                         " and id NOT IN (SELECT TOP " + page * 50 + " id FROM crm_customer ORDER BY customerno) ORDER BY customerno"; //显示前50条信息        


               da.Text = sql;
               da.Fill(dt);
               for (int x = 0; x < dt.Rows.Count; x++)
               {
                   count = dt.Rows[x][17].ToString();
                   if (count == "0")
                       count = "";
                   else
                       count = "(" + count + ")";
                   string name = dt.Rows[x][0].ToString() + count;
                   string emailAddress = dt.Rows[x][16].ToString();
                   if (name == "")
                   {
                       string[] emailList = Regex.Split(emailAddress, ",", RegexOptions.IgnoreCase);
                       string email = emailList[0];
                       lvi = new ListViewItem(email);
                   }
                   else
                       lvi = new ListViewItem(name);
                   for (int k = 1; k < dt.Columns.Count; k++)
                   {
                       lvi.SubItems.Add(dt.Rows[x][k].ToString());
                   }
                   
                   this.listEmailUser.Items.Add(lvi);
               }
               this.listEmailUser.Items.Add("");
               this.listEmailUser.Items.Add("");
           }
       }
       
        /// 
        /// 下一页
        /// 
        /// 
        /// 
       private void btnNextPage_Click(object sender, EventArgs e)
       {
           //this.listEmailUser.Clear();
           string status = "3";
           //当点击下一页时,上一页是可用的。
           btnPrevPage.Enabled = true;
           //记录有可能是被50整除的,有可能是不能被50整除,所以判断下一页是否可用需要分情况
           if (totalcount % 50 == 0)
           {
               if (page >= totalcount / 50 - 2)
               {
                   btnNextPage.Enabled = false;
                   btnPrevPage.Enabled = true;
               }
           }
           else
           {
               if (page >= totalcount / 50 - 1)
               {
                   btnNextPage.Enabled = false;
                   btnPrevPage.Enabled = true;
               }
           }
           page = page + 1;//下一页

           listEmailUser.Items.Clear();//清空上一页的数据加载下一页的数据
           string count = "";
           using (DataAdapter da = new DataAdapter())
           {
               DataTable dt = new DataTable();
               sql = "SELECT top 50 linkman,customerno,[address],FAX,POSTCODE,COUNTRYID,PHONE,shortname,CompanyName , " +
                                  "(select name from  bas_customerstyle where id=style) as style," +
                                  "(select name from  bas_customerstatus where id=CustomerStatus ) as CustomerStatus," +
                                  "(select cnname  from bas_region where id=Regionid ) as Region," +
                                  "(select cnname from bas_country where id=Countryid ) as Country," +
                                  "(select cnname from bas_province where id=Provinceid ) as Province," +
                                  "(select name   from bas_customercredit where ID=CreditRateid) as CreditRate," +
                                  "(select name  from bas_sysstatus  where ID=sysstatus ) as sysstatus ," +
                                  "dbo.GetEmailAddress(a.id) as emailaddress ,(select COUNT(*) as ecount from E_EmailInfo where E_SendId =CAST(a.customerno as varchar(50))  and (E_ReplyId = '" + Erp.PDM.User.My.UID + "' or E_CCId = '" + Erp.PDM.User.My.UID + "') and IsRead = 'false' and SUBSTRING(E_EmailInfo.E_SendId,1,1) = 'C') as ecount" +
                                         " FROM crm_customer a where 1=1 and CustomerStatus ='" + status + "'" +
                                         " and id NOT IN (SELECT TOP " + page * 50 + " id FROM crm_customer ORDER BY customerno) ORDER BY customerno"; //显示前50条信息        


               da.Text = sql;
               da.Fill(dt);
               for (int x = 0; x < dt.Rows.Count; x++)
               {
                   count = dt.Rows[x][17].ToString();
                   if (count == "0")
                       count = "";
                   else
                       count = "(" + count + ")";
                   string name = dt.Rows[x][0].ToString() + count;
                   string emailAddress = dt.Rows[x][16].ToString();
                   if (name == "")
                   {
                       string[] emailList = Regex.Split(emailAddress, ",", RegexOptions.IgnoreCase);
                       string email = emailList[0];
                       lvi = new ListViewItem(email);
                   }
                   else
                       lvi = new ListViewItem(name);
                   for (int k = 1; k < dt.Columns.Count; k++)
                   {
                       lvi.SubItems.Add(dt.Rows[x][k].ToString());
                   }
                   this.listEmailUser.Items.Add(lvi);
               }
               this.listEmailUser.Items.Add("");
               this.listEmailUser.Items.Add("");
           }
       }
    }
}




你可能感兴趣的:(C# winform listview分页(sql分页))