表名为people;
int a=10; --每页放10行数据;
构造函数内:
using (DBDataContext con = new DBDataContext()) { List<people> pl = con.people.Skip(0).Take(a).ToList(); foreach (people x in pl) { ListViewItem l = new ListViewItem(); l.Text = x.ids.ToString(); l.SubItems.Add(x.name); l.SubItems.Add(x.pwd); l.SubItems.Add(x.nickname); l.SubItems.Add(x.sex.Value ? "男" : "女"); l.SubItems.Add(x.nation.nationname); l.SubItems.Add(x.birthday.Value.ToString("yyyy年MM月dd日")); listView1.Items.Add(l); } }
查询最大页数:
public int maxy() { int x = 0; using (DBDataContext con = new DBDataContext()) { x = con.people.ToList().Count; if (x % 10 == 0) { x = x / 10; } else { x = x / 10 + 1; } } return x; }
上一页按钮:
private void button1_Click(object sender, EventArgs e) { if (label2.Text != "1") { listView1.Items.Clear(); using(DBDataContext con=new DBDataContext()) { List<people> pl = con.people.Skip((Convert.ToInt32(label2.Text) - 2) * a).Take(a).ToList(); foreach (people x in pl) { ListViewItem l = new ListViewItem(); l.Text = x.ids.ToString(); l.SubItems.Add(x.name); l.SubItems.Add(x.pwd); l.SubItems.Add(x.nickname); l.SubItems.Add(x.sex.Value ? "男" : "女"); l.SubItems.Add(x.nation.nationname); l.SubItems.Add(x.birthday.Value.ToString("yyyy年MM月dd日")); listView1.Items.Add(l); } } int i = Convert.ToInt32(label2.Text); i--; label2.Text = i.ToString(); } }
下一页按钮:
private void button2_Click(object sender, EventArgs e) { int y = maxy(); if (Convert.ToInt32(label2.Text) != y) { listView1.Items.Clear(); using (DBDataContext con = new DBDataContext()) { List<people> pl = con.people.Skip(Convert.ToInt32(label2.Text) * a).Take(a).ToList(); foreach (people x in pl) { ListViewItem l = new ListViewItem(); l.Text = x.ids.ToString(); l.SubItems.Add(x.name); l.SubItems.Add(x.pwd); l.SubItems.Add(x.nickname); l.SubItems.Add(x.sex.Value ? "男" : "女"); l.SubItems.Add(x.nation.nationname); l.SubItems.Add(x.birthday.Value.ToString("yyyy年MM月dd日")); listView1.Items.Add(l); } } int i = Convert.ToInt32(label2.Text); i++; label2.Text = i.ToString(); } }