时间:2021-07-01 10:21:17 帮助过:5人阅读
后台
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Windows; using System.Windows.Controls; using System.Windows.Data; using System.Windows.Documents; using System.Windows.Input; using System.Windows.Media; using System.Windows.Media.Imaging; using System.Windows.Shapes; using System.Data; namespace Supplier { /// <summary> /// Edit.xaml 的交互逻辑 /// </summary> public partial class UserI : Window { public int pageSize = Global.PageSize; //每页记录数 private int recordsum = 0; //表中记录总数 private int PageSum = 0; //总页数 private int CurrentPage = 1; //当前页 private List<User> CurrentList; public UserI() { InitializeComponent(); this.Topmost = true;//顶置窗口 } private void Window_Loaded(object sender, RoutedEventArgs e) { //pageIndex 当前页的序号 recordsum = UserManager.getCount(); //判断总页数 if (recordsum % pageSize == 0) PageSum = recordsum / pageSize; else PageSum = recordsum / pageSize + 1; DataGridBind(CurrentPage); } #region 用户操作 private void updateUser(object sender, RoutedEventArgs e) { UserEdit userEdit = new UserEdit(); Button bt = e.OriginalSource as Button; User u = bt.Tag as User; userEdit.user = u; userEdit.hasType = true; bool? result = userEdit.ShowDialog(); if (result != null && (bool)result) { Window_Loaded(sender, e); DataGridBind(CurrentPage); } } private void deleteUser(object sender, RoutedEventArgs e) { if (MessageBox.Show("您确定要删除该用户吗?", "系统提示", MessageBoxButton.YesNo, MessageBoxImage.Warning) == MessageBoxResult.Yes) { UserEdit userEdit = new UserEdit(); Button bt = e.OriginalSource as Button; User u = bt.Tag as User; UserManager.delete(u.ID); Window_Loaded(sender, e); DataGridBind(CurrentPage); } } private void addUser(object sender, RoutedEventArgs e) { UserAdd userAdd = new UserAdd(); userAdd.hasType = true; bool? result = userAdd.ShowDialog(); if (result != null && (bool)result) { Window_Loaded(sender, e); DataGridBind(PageSum); } } #endregion #region 分页 private void DataGridBind(int pageIndex) { if (pageIndex > 0) { CurrentList = UserManager.getList((pageIndex - 1) * pageSize, pageSize); this.grid.ItemsSource = CurrentList; CurrentPage = pageIndex; label1.Content = CurrentPage; label2.Content = PageSum; } } /// <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(); } } } private void textBox1_KeyDown(object sender, KeyEventArgs e) { if (e.Key == Key.Enter) JumpPage(sender, e); } #endregion } }View Code
数据操作model 层
/// <summary> /// 获取第skipNum到(skipNum+pageSize)内的记录 /// </summary> /// <param name="skipNum"></param> /// <param name="pageSize"></param> /// <returns></returns> public static List<User> getList(int skipNum, int pageSize) { string sql = "select top " + pageSize + " Number= row_number() over(order by id asc), * from Users where id not in " + "(select top " + skipNum + " id from Users order by id asc) order by id asc"; DataTable dataTable = DbConHelper.ExecuteDataTable(sql); List<User> userList = new List<User>(); User userModel; if (dataTable.Rows.Count > 0) { int page = skipNum / pageSize+1; foreach (DataRow row in dataTable.Rows) { userModel = new User() { ID = row["ID"].ToString().Trim(), Number = (int.Parse(row["Number"].ToString().Trim()) * page).ToString(), Password = row["Password"].ToString().Trim(), Type = row["Type"].ToString().Trim(), UserName = row["UserName"].ToString().Trim() }; userList.Add(userModel); } } return userList; } public static int getCount() { string sql = "select count(*) from Users"; return (int)DbConHelper.ExecuteScalar(sql); }View Code
数据库之 :分页技术:之点击一次取一次数据
标签: