Devexpress for WPF Grid分页
Devexpress for WPF中没有找到分页控件,列表数据太多,不分页肯定是不行的,所以就写了一个。
感觉还不错。这里跟大家分享一下
思路说明:首先创建自定义控件,控制好分页效果等。核心的位置便是分页信息与Grid的交互,这里使用事件委托完成。
分页控件做成这个效果,比较粗糙,后面有时间在更新一下。
界面代码就不上传了。
后台代码给大家看看:
public DataPager() { InitializeComponent(); cboPageSize.SelectionChanged += cboPageSize_SelectionChanged; } public delegate void BackToCallback(int start, int num, ref int count); public event BackToCallback OnPageGetList; public int pageIndex = 1; public int num = 100; public int PageCount = 1; public int count; void cboPageSize_SelectionChanged(object sender, SelectionChangedEventArgs e) { try { ComboBoxItem cbb = cboPageSize.SelectedItem as ComboBoxItem; num = Convert.ToInt32(cbb.Content); int iEnd = count % num == 0 ? count / num : count / num + 1; pageIndex = Convert.ToInt32(txtJumpPage.Text); if (pageIndex > iEnd) { pageIndex = iEnd; } DgvListBind(pageIndex, num, ref count); ImgControl(pageIndex, count); } catch (Exception ex) { } } private void DgvListBind(int pageIndex, int num, ref int count) { if (OnPageGetList != null) { int start = (pageIndex - 1) * num; OnPageGetList(start, num, ref count); if (count == 0) { lblPageCount.Content = "1"; lblTotal.Content = count.ToString(); } else { lblPageCount.Content = (count % num == 0 ? count / num : count / num + 1).ToString(); lblTotal.Content = count.ToString(); } } } private void ImgControl(int pageIndex, int pageCount) { pageCount = pageCount / num == 0 ? pageCount / num : (pageCount / num) + 1; if (pageIndex <= 1 && pageCount <= 1) { btnFirst.IsEnabled = false; btnLeft.IsEnabled = false; btnRight.IsEnabled = false; btnLast.IsEnabled = false; } else if (pageIndex == 1 && pageCount > 1) { btnFirst.IsEnabled = false; btnLeft.IsEnabled = false; btnRight.IsEnabled = true; btnLast.IsEnabled = true; } else if (pageIndex > 1 && pageIndex < pageCount) { btnFirst.IsEnabled = true; btnLeft.IsEnabled = true; btnRight.IsEnabled = true; btnLast.IsEnabled = true; } else if (pageIndex > 1 && pageIndex == pageCount) { btnFirst.IsEnabled = true; btnLeft.IsEnabled = true; btnRight.IsEnabled = false; btnLast.IsEnabled = false; } } public void btnFirst_Click(object sender, RoutedEventArgs e) { pageIndex = 1; txtJumpPage.Text = "1"; num = Convert.ToInt32(cboPageSize.Text); DgvListBind(pageIndex, num, ref count); ImgControl(pageIndex, count); } private void btnLeft_Click(object sender, RoutedEventArgs e) { pageIndex -= 1; txtJumpPage.Text = pageIndex.ToString(); num = Convert.ToInt32(cboPageSize.Text); DgvListBind(pageIndex, num, ref count); ImgControl(pageIndex, count); } private void btnRight_Click(object sender, RoutedEventArgs e) { pageIndex += 1; txtJumpPage.Text = pageIndex.ToString(); num = Convert.ToInt32(cboPageSize.Text); DgvListBind(pageIndex, num, ref count); ImgControl(pageIndex, count); } private void btnLast_Click(object sender, RoutedEventArgs e) { num = Convert.ToInt32(cboPageSize.Text); pageIndex = count % num == 0 ? count / num : count / num + 1; DgvListBind(pageIndex, num, ref count); //也许有新数据所以要更新 pageIndex = count % num == 0 ? count / num : count / num + 1; txtJumpPage.Text = pageIndex.ToString(); DgvListBind(pageIndex, num, ref count); ImgControl(pageIndex, count); } private void btnJump_Click(object sender, RoutedEventArgs e) { int index = 0; try { index = Convert.ToInt32(txtJumpPage.Text); } catch (Exception ex) { MessageBox.Show("请输入数字!"); return; } num = Convert.ToInt32(cboPageSize.Text); int end = count % num == 0 ? count / num : count / num + 1; if (index > end) { index = end; txtJumpPage.Text = end.ToString(); } pageIndex = index; DgvListBind(pageIndex, num, ref count); txtJumpPage.Text = pageIndex.ToString(); DgvListBind(pageIndex, num, ref count); ImgControl(pageIndex, count); } /// <summary> /// 刷新 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> public void btnRefresh_Click(object sender, RoutedEventArgs e) { DgvListBind(pageIndex, num, ref count); }
调用很简单,
在调用这个控件的地方,实现委托OnPageGetList就好了Demo代码下载