电商直播系统源码,利用DatagridView BindingNavigato实现分页的相关代码
List<person> personlist =new List<person>(); int pageSize = 20; int pageCount = 0; private void Form1_Load(object sender, EventArgs e) { for (int i = 0; i < 112; i++) { person p = new person(); p.name = "test" + i.ToString(); p.age = i; personlist.Add(p); } LoadData(1, dgvDemo); bindingNavigatorPositionItem.Enabled = true; // Set the status of the BindingNavigator control if (pageCount == 0 || pageCount == 1) { bindngrDemo.MoveFirstItem.Enabled = false; bindngrDemo.MoveLastItem.Enabled = false; bindngrDemo.MoveNextItem.Enabled = false; bindngrDemo.MovePreviousItem.Enabled = false; } else { bindngrDemo.MoveFirstItem.Enabled = false; bindngrDemo.MoveLastItem.Enabled = true; bindngrDemo.MoveNextItem.Enabled = true; bindngrDemo.MovePreviousItem.Enabled = false; bindngrDemo.PositionItem.Text = "1"; bindngrDemo.CountItem.Text = "of {" + pageCount.ToString() + "}"; } } private void LoadData(int pageIndex, DataGridView dgvDemo) { int Count = personlist.Count(); if (Count % pageSize == 0) pageCount = Count / pageSize; else pageCount = Count / pageSize + 1; List<person> plist = personlist.Skip((pageIndex-1) * pageSize).Take(pageSize).ToList(); BindingSource bindseDemo = new BindingSource(); bindseDemo.DataSource = plist; dgvDemo.DataSource = bindseDemo; } private void bindingNavigatorMoveFirstItem_Click(object sender, EventArgs e) { LoadData(1, dgvDemo); bindngrDemo.PositionItem.Text = "1"; bindngrDemo.MoveFirstItem.Enabled = false; bindngrDemo.MovePreviousItem.Enabled = false; bindngrDemo.MoveNextItem.Enabled = true; bindngrDemo.MoveLastItem.Enabled = true; } private void bindingNavigatorMovePreviousItem_Click(object sender, EventArgs e) { int currentPage = Convert.ToInt32(bindngrDemo.PositionItem.Text); if (currentPage >= 2) { int page = currentPage - 1; bindngrDemo.PositionItem.Text = page.ToString(); LoadData(page, dgvDemo); if (page == 1) { bindngrDemo.MovePreviousItem.Enabled = false; bindngrDemo.MoveFirstItem.Enabled = false; } if (page <= pageCount) { bindngrDemo.MoveNextItem.Enabled = true; bindngrDemo.MoveLastItem.Enabled = true; } } else { MessageBox.Show("This is the first page", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information); } } private void bindingNavigatorMoveNextItem_Click(object sender, EventArgs e) { int currentPage = Convert.ToInt32(bindngrDemo.PositionItem.Text); if (currentPage < pageCount) { int page = currentPage + 1; bindngrDemo.PositionItem.Text = page.ToString(); LoadData(page, dgvDemo); if (page == pageCount) { bindngrDemo.MoveNextItem.Enabled = false; bindngrDemo.MoveLastItem.Enabled = false; } if (page >= 2) { bindngrDemo.MovePreviousItem.Enabled = true; bindngrDemo.MoveFirstItem.Enabled = true; } } else { MessageBox.Show("This is the last page", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information); } } private void bindingNavigatorMoveLastItem_Click(object sender, EventArgs e) { LoadData(pageCount, dgvDemo); bindngrDemo.PositionItem.Text = pageCount.ToString(); bindngrDemo.MoveLastItem.Enabled = false; bindngrDemo.MoveNextItem.Enabled = false; bindngrDemo.MovePreviousItem.Enabled = true; bindngrDemo.MoveFirstItem.Enabled = true; } private void bindingNavigatorPositionItem_KeyPress(object sender, KeyPressEventArgs e) { if ((e.KeyChar >= 48 && e.KeyChar <= 57) || e.KeyChar == 8 || e.KeyChar == 13 || e.KeyChar == 127) { e.Handled = false; if (e.KeyChar == 13) { int page=Convert.ToInt16(bindingNavigatorPositionItem.Text); LoadData(page, dgvDemo); } } else { e.Handled = true; } } } public class person { public string name { get; set; } public int age { get; set; } }
以上就是 电商直播系统源码,利用DatagridView BindingNavigato实现分页的相关代码,更多内容欢迎关注之后的文章