自己正在做邮件系统遇到了的listview分页功能的问题,来和大家分享下,话不多说,直接贴代码
下面是界面代码,
namespace Erp.PDM.MulMail { partial class TestPage { ///
以下是后台代码,简单易懂,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("");
}
}
}
}