博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
数据库之 :分页技术:之点击一次取一次数据
阅读量:5058 次
发布时间:2019-06-12

本文共 6722 字,大约阅读时间需要 22 分钟。

前台xaml文件

View Code

后台

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{    ///     /// Edit.xaml 的交互逻辑    ///     public partial class UserI : Window    {        public int pageSize = Global.PageSize; //每页记录数        private int recordsum = 0;   //表中记录总数        private int PageSum = 0;     //总页数         private int CurrentPage = 1; //当前页         private List
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; } } ///
/// 上一页 /// ///
///
private void PreviousPage(object sender, RoutedEventArgs e) { if (CurrentPage > 1) DataGridBind(CurrentPage - 1); } ///
/// 下一页 /// ///
///
private void NextPage(object sender, RoutedEventArgs e) { if (CurrentPage < PageSum) DataGridBind(CurrentPage + 1); } ///
/// 跳转页 /// ///
///
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 层

///         /// 获取第skipNum到(skipNum+pageSize)内的记录        ///         ///         ///         /// 
public static List
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
userList = new List
(); 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

 

转载于:https://www.cnblogs.com/lizhenlin/p/5877289.html

你可能感兴趣的文章
存储开头结尾使用begin tran,rollback tran作用?
查看>>
Activity启动过程中获取组件宽高的五种方式
查看>>
java导出Excel表格简单的方法
查看>>
SQLite数据库简介
查看>>
利用堆实现堆排序&amp;优先队列
查看>>
Mono源码学习笔记:Console类(四)
查看>>
Android学习路线(十二)Activity生命周期——启动一个Activity
查看>>
《Genesis-3D开源游戏引擎完整实例教程-跑酷游戏篇03:暂停游戏》
查看>>
CPU,寄存器,一缓二缓.... RAM ROM 外部存储器等简介
查看>>
windows下编译FreeSwitch
查看>>
git .gitignore 文件不起作用
查看>>
Alan Turing的纪录片观后感
查看>>
c#自定义控件中的事件处理
查看>>
App.config自定义节点读取
查看>>
unity3d根据手机串号和二维码做正版验证
查看>>
二十六、Android WebView缓存
查看>>
django Models 常用的字段和参数
查看>>
linux -- 嵌入式linux下wifi无线网卡驱动
查看>>
SVN使用教程总结
查看>>
SQL中varchar和nvarchar有什么区别?
查看>>