分页功能
1.变量
/// <summary> /// 每页记录数 /// </summary> public int pageSize = Global.UserPageSize; /// <summary> /// 表中记录总数 /// </summary> private int recordsum = 0; /// <summary> /// 总页数 /// </summary> private int PageSum = 0; /// <summary> /// 当前页 /// </summary> private int CurrentPage = 1; /// <summary> /// 当前显示记录集合 /// </summary> private List<User> CurrentList;
2.初始化分页的总记录数、总页数
/// <summary> /// 初始化分页 页数 /// </summary> /// <param name="userName"></param> /// <param name="userType"></param> private void InitPage() { right = this.permission.SelectedItem as UserRight; if (right != null && !string.IsNullOrEmpty(right.Value)) { //pageIndex 当前页的序号 recordsum = UserManager.getCount(SearchName, right.Value); } else { recordsum = UserManager.getCount(SearchName); } //判断总页数 if (recordsum % pageSize == 0) PageSum = recordsum / pageSize; else PageSum = recordsum / pageSize + 1; }
3.初始化分页集合
private void DataGridBind(int pageIndex) { if (pageIndex > 0) { right = this.permission.SelectedItem as UserRight; if (right != null && !string.IsNullOrEmpty(right.Value)) { CurrentList = UserManager.getList((pageIndex - 1) * pageSize, pageSize, SearchName, right.Value); } else CurrentList = UserManager.getList((pageIndex - 1) * pageSize, pageSize, SearchName); this.grid.ItemsSource = CurrentList; CurrentPage = pageIndex; label1.Content = CurrentPage; label2.Content = PageSum; total.Content = recordsum; } }
4.跳转页
/// <summary> /// 上一页 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void PreviousPage(object sender, RoutedEventArgs e) { if (CurrentPage > 1) DataGridBind(CurrentPage - 1); } /// <summary> /// 下一页 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void NextPage(object sender, RoutedEventArgs e) { if (CurrentPage < PageSum) DataGridBind(CurrentPage + 1); } /// <summary> /// 跳转页 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void JumpPage(object sender, RoutedEventArgs e) { int page = 0; if (int.TryParse(textBox1.Text, out page)) { if (page <= PageSum) DataGridBind(page); else { MessageBox.Show("对不起,您输入的页面超过当前最大页数", "提示", MessageBoxButton.OK, MessageBoxImage.Information); this.textBox1.Clear(); } } }
5.调用