[DevExpress]GridControl分页的实现
加入两个组件:BindingNavigator和BindingSource
代码:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Text;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using DZAMS.DBUtility; namespace DZAMS.Demo
{ public partial class GridPage_Frm : DevExpress.XtraEditors.XtraForm
{
public DataTable dt = new DataTable();
StoreProcedure sp;
private int pageSize = 10; //每页显示行数
private int nMax = 0; //总记录数
private int pageCount = 0; //页数=总记录数/每页显示行数
private int pageCurrent = 0; //当前页号
private DataSet ds = new DataSet();
private DataTable dtInfo = new DataTable();
public GridPage_Frm()
{
InitializeComponent();
} private void GridPage_Frm_Load(object sender, EventArgs e)
{
string strQuery = string.Format("SELECT Id, UserCode, UserName, RoleName, Ip, Mac, LoginTime FROM DZ_LoginLog");
dt = SqlHelper.ExecuteDataset(SqlHelper.conn, CommandType.Text, strQuery.ToString()).Tables[0]; gridControl1.DataSource = dt; string strConn = "SERVER=(local);DATABASE=DZ;UID=sa;PWD=XXXX"; //数据库连接字符串
SqlConnection conn = new SqlConnection(strConn);
conn.Open();
string strSql = "SELECT count(*) as num FROM DZ_LoginLog";
SqlDataAdapter sda = new SqlDataAdapter(strSql, conn);
sda.Fill(ds, "ds");
conn.Close(); nMax = Convert.ToInt32(ds.Tables[0].Rows[0]["num"].ToString());
lblTotalCount.Text = nMax.ToString();
lblPageSize.Text = pageSize.ToString(); sp = new StoreProcedure("Pr_Monitor_Pagination", strConn);
dtInfo = sp.ExecuteDataTable("DZ_LoginLog", "Id", "Id desc", pageCurrent++, pageSize);
InitDataSet(); }
private void InitDataSet()
{
pageCount = (nMax / pageSize); //计算出总页数 if ((nMax % pageSize) > 0) pageCount++; pageCurrent = 1; //当前页数从1開始 LoadData();
} private void LoadData()
{
lblPageCount.Text = "/"+pageCount.ToString();
txtCurrentPage.Text = Convert.ToString(pageCurrent); this.bdsInfo.DataSource = dtInfo;
this.bdnInfo.BindingSource = bdsInfo;
this.gridControl1.DataSource = bdsInfo;
} private void bdnInfo_ItemClicked(object sender, ToolStripItemClickedEventArgs e)
{
if (e.ClickedItem.Text == "导出当前页")
{
SaveFileDialog saveFileDialog = new SaveFileDialog();
saveFileDialog.Title = "导出Excel";
saveFileDialog.Filter = "Excel文件(*.xls)|*.xls";
DialogResult dialogResult = saveFileDialog.ShowDialog(this);
if (dialogResult == DialogResult.OK)
{
DevExpress.XtraPrinting.XlsExportOptions options = new DevExpress.XtraPrinting.XlsExportOptions();
gridControl1.ExportToXls(saveFileDialog.FileName, options);
// gridControl1.ExportToExcelOld(saveFileDialog.FileName);
DevExpress.XtraEditors.XtraMessageBox.Show("保存成功!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
}
if (e.ClickedItem.Text == "关闭")
{
this.Close();
}
if (e.ClickedItem.Text == "首页")
{
pageCurrent--;
if (pageCurrent <= 0)
{
MessageBox.Show("已经是首页。请点击“下一页”查看!");
return;
}
else
{
pageCurrent = 1;
dtInfo = sp.ExecuteDataTable("DZ_LoginLog", "Id", "Id desc", pageCurrent, pageSize);
}
}
if (e.ClickedItem.Text == "上一页")
{
pageCurrent--;
if (pageCurrent <= 0)
{
MessageBox.Show("已经是第一页,请点击“下一页”查看!");
return;
}
else
{
dtInfo = sp.ExecuteDataTable("DZ_LoginLog", "Id", "Id desc", pageCurrent, pageSize);
}
}
if (e.ClickedItem.Text == "下一页")
{
pageCurrent++;
if (pageCurrent > pageCount)
{
MessageBox.Show("已经是最后一页。请点击“上一页”查看。");
return;
}
else
{
dtInfo = sp.ExecuteDataTable("DZ_LoginLog", "Id", "Id desc", pageCurrent, pageSize);
}
}
if (e.ClickedItem.Text == "尾页")
{
pageCurrent++;
if (pageCurrent > pageCount)
{
MessageBox.Show("已经是尾页,请点击“上一页”查看。");
return;
}
else
{
pageCurrent = pageCount;
dtInfo = sp.ExecuteDataTable("DZ_LoginLog", "Id", "Id desc", pageCount, pageSize);
}
}
LoadData();
} }
}
StoreProcedure类:
public class StoreProcedure
{
// 存储过程名称。
private string _name;
// 数据库连接字符串。 private string _conStr;
// 构造函数
// sprocName: 存储过程名称;
// conStr: 数据库连接字符串。 public StoreProcedure(string sprocName, string conStr)
{
_conStr = conStr;
_name = sprocName;
}
// 运行存储过程,不返回值。
// paraValues: 參数值列表。
// return: void
public void ExecuteNoQuery(params object[] paraValues)
{
using (SqlConnection con = new SqlConnection(_conStr))
{
SqlCommand comm = new SqlCommand(_name, con);
comm.CommandType = CommandType.StoredProcedure;
AddInParaValues(comm, paraValues);
con.Open();
comm.ExecuteNonQuery();
con.Close();
}
} // 运行存储过程返回一个表。
// paraValues: 參数值列表。
// return: DataTable
public DataTable ExecuteDataTable(params object[] paraValues)
{
SqlCommand comm = new SqlCommand(_name, new SqlConnection(_conStr));
comm.CommandType = CommandType.StoredProcedure;
AddInParaValues(comm, paraValues);
SqlDataAdapter sda = new SqlDataAdapter(comm);
DataTable dt = new DataTable();
sda.Fill(dt);
return dt;
} // 运行存储过程。返回SqlDataReader对象。
// 在SqlDataReader对象关闭的同一时候。数据库连接自己主动关闭。 // paraValues: 要传递给给存储过程的參数值类表。
// return: SqlDataReader
public SqlDataReader ExecuteDataReader(params object[] paraValues)
{
SqlConnection con = new SqlConnection(_conStr);
SqlCommand comm = new SqlCommand(_name, con);
comm.CommandType = CommandType.StoredProcedure;
AddInParaValues(comm, paraValues);
con.Open();
return comm.ExecuteReader(CommandBehavior.CloseConnection);
}
// 获取存储过程的參数列表。
private ArrayList GetParas()
{
SqlCommand comm = new SqlCommand("dbo.sp_sproc_columns_90",
new SqlConnection(_conStr));
comm.CommandType = CommandType.StoredProcedure;
comm.Parameters.AddWithValue("@procedure_name", (object)_name);
SqlDataAdapter sda = new SqlDataAdapter(comm);
DataTable dt = new DataTable();
sda.Fill(dt);
ArrayList al = new ArrayList();
for (int i = 0; i < dt.Rows.Count; i++)
{
al.Add(dt.Rows[i][3].ToString());
}
return al;
}
// 为 SqlCommand 加入參数及赋值。
private void AddInParaValues(SqlCommand comm, params object[] paraValues)
{
comm.Parameters.Add(new SqlParameter("@RETURN_VALUE", SqlDbType.Int));
comm.Parameters["@RETURN_VALUE"].Direction =
ParameterDirection.ReturnValue;
if (paraValues != null)
{
ArrayList al = GetParas();
for (int i = 0; i < paraValues.Length; i++)
{
comm.Parameters.AddWithValue(al[i + 1].ToString(),
paraValues[i]);
}
}
}
}
存储过程:
ALTER procedure [dbo].[Pr_Monitor_Pagination]
-- ============================================= == Paging == =============================================
--Author: Lee
--Create date: 2010\06\11
--Parameter:
-- 1.Tables :The Name Of Table or view
-- 2.PrimaryKey :Primary Key
-- 3.Sort :Ordering Statement,Without Order By, For Example:NewsID Desc,OrderRows Asc
-- 4.CurrentPage :The Page Number Of Current page
-- 5.PageSize :The Size Of One Page's Group
-- 6.Fields :The Field Of You Needed
-- 7.Filter :Where Condition,Without Where
-- 8.Group :Group Condition。Without Group By
-- 9.GetCount :Return The Number Of All, Not Zero
--Updates:
-- 2010\06\09 Create Procedure.
-- ========================================================================================================
@Tables varchar(600),
@PrimaryKey varchar(100),
@Sort varchar(200)=null,
@CurrentPage bigint=1,
@PageSize bigint=10,
@Fields varchar(1000)='*',
@Filter varchar(1000)=null,
@Group varchar(1000)=null,
@GetCount bit=0
as
if(@GetCount=0)
begin
/*Ordering Of Default */
if @Sort is null or @Sort=''
set @Sort=@PrimaryKey
declare @SortTable varchar(100)
declare @SortName varchar(100)
declare @strSortColumn varchar(200)
declare @operator char(2)
declare @type varchar(100)
declare @prec int
/*Setting Condition Of Ordering*/
if charindex('desc',@Sort)>0
begin
set @strSortColumn=replace(@Sort,'desc','')
set @operator='<='
end
else
begin
if charindex('asc',@Sort)=0
set @strSortColumn=replace(@Sort,'asc','')
set @operator='>='
end
if charindex('.',@strSortColumn)>0
begin
set @SortTable=substring(@strSortColumn,0,charindex('.',@strSortColumn))
set @SortName=substring(@strSortColumn,charindex('.',@strSortColumn)+1,len(@strSortColumn))
end
else
begin
set @SortTable=@Tables
set @SortName=@strSortColumn
end
select @type=t.name,@prec=c.prec from sysobjects o join syscolumns c on o.id=c.id join systypes t on c.xusertype=t.xusertype where o.name=@SortTable and c.name=@SortName
if charindex('char',@type)>0
set @type=@type+'('+cast(@prec as varchar)+')'
declare @strPageSize varchar(50)
declare @strStartRow varchar(50)
declare @strFilter varchar(1000)
declare @strSimpleFilter varchar(1000)
declare @strGroup varchar(1000)
/*CurrentPage Of Default*/
if @CurrentPage<1
set @CurrentPage=1
/*Setting Paging param*/
set @strPageSize=cast(@PageSize as varchar(50))
set @strStartRow=cast(((@CurrentPage-1)*@PageSize+1) as varchar(50))
/*Condition Of Filter And Group*/
if @Filter is not null and @Filter!=''
begin
set @strFilter=' where '+@Filter+' '
set @strSimpleFilter=' and '+@Filter +' '
end
else
begin
set @strSimpleFilter=''
set @strFilter=''
end
if @Group is not null and @Group!=''
set @strGroup=' group by '+@Group+' '
else
set @strGroup=''
exec(' declare @SortColumn '+ @type + ' set RowCount ' + @strStartRow+ ' select @SortColumn=' + @strSortColumn + ' from ' + @Tables+ @strFilter + ' ' + @strGroup + ' Order by ' + @Sort+ ' set rowcount ' + @strPageSize + ' select ' + @Fields + ' from ' + @Tables + ' where ' + @strSortColumn + @operator+ '@SortColumn ' + @strSimpleFilter + ' ' + @strGroup + ' Order by ' + @Sort + ' ')
end
else
begin
declare @strSQL varchar(5000)
if @Filter !=''
set @strSQL = 'select count(' + @PrimaryKey + ') as Total from [' + @Tables + '] where ' + @Filter
else
set @strSQL = 'select count(' + @PrimaryKey + ') as Total from [' + @Tables + ']'
exec(@strSQL)
end
效果:
[DevExpress]GridControl分页的实现的更多相关文章
- C# devexpress gridcontrol 分页 控件制作
这个小小的功能实现起来还是有一点点复杂, 分页单独一个usercontrol 出来,导致查询换页 与gridcontrol页面分离, 一般通过换页事件通知girdcontrol 做出查询 查询来说有 ...
- DevExpress GridControl+UserControl实现分页
志向不过是记忆的奴隶,生气勃勃地降生,但却很难成长. —— 莎士比亚 时隔一年,我写随笔真的很随意,想起了就来博客园写写,想不起来就任由懒惰支配着我.不过我到觉得这不是什么坏事,你不用为了完成某事而让 ...
- DevExpress GridControl使用(转)
DevExpress GridControl使用 (一)原汁原味的表格展示 Dev控件中的表格控件GridControl控件非常强大.不过,一些细枝末节的地方有时候用起来不好找挺讨厌的.使用过程中,多 ...
- DevExpress GridControl使用方法
一.如何解决单击记录整行选中的问题 View->OptionsBehavior->EditorShowMode 设置为:Click 二.如何新增一条记录 (1).gridView.AddN ...
- 实现DevExpress GridControl 只有鼠标双击后才进行修改数据
1. 实现DevExpress GridControl 只有鼠标双击后才进行修改数据:修改GridView.OptionsBehavior.EditorShowMode属性为Click 2. 实现De ...
- DevExpress GridControl 使用方法技巧 总结 收录整理
一.如何解决单击记录整行选中的问题 View->OptionsBehavior->EditorShowMode 设置为:Click 二.如何新增一条记录 ().gridView.AddNe ...
- Devexpress GridControl z
http://minmin86121.blog.163.com/blog/static/4968115720144194923578/ 1 AllowNullInput=False; --Devexp ...
- DevExpress GridControl 单元格添加进度条(ProgressBar)
首先可以使用DevExpress GridControl 自带的进度条控件. 但是我要用一个方法来设置所以的单元格进度,而不是每个单元格都要设置一遍,同时我想要根据进度值不同,进度条显示不同的颜色. ...
- DevExpress GridControl 显示外部图片
如果数据源中只包含图片的链接,如何在DevExpress GridControl的一列中显示外部图片? 要实现该功能,可通过非绑定列的方式来实现.具体实现方法如下: 1. 创建了一个非绑定列并设 ...
随机推荐
- Cloudera’s Distribution Including Apache Hadoop(CDH)安装过程
文档地址:https://www.cloudera.com/documentation.html https://www.cloudera.com/documentat ...
- CSS Paint API绘制透明格子背景实例页面
CSS代码: .box { width: 180px; height: 180px; background: paint(transparent-grid); } HTML代码: <div cl ...
- js 鼠标拖拽元素移动
<!DOCTYPE html><html> <head> <title> </title> <style media="sc ...
- 获取url上的参数
var aa = '?name=hss&age=13'; function strToObj(str){ if(typeof str === 'undefi ...
- Python学习-列表的转换和增加操作
列表的转换和增加操作 列表的转换操作:可以将一个元组,字符串等转换成列表. str = "liuwenhao"; print(list(str)); // ['l', 'i', ' ...
- 搭建分布式yarn
1.在前一篇准备好Hadoop的基础上配置,链接 http://www.cnblogs.com/cici20166/p/6266367.html 2./etc/profile 配置环境变量 expor ...
- POJ 2718 Smallest Difference(贪心 or next_permutation暴力枚举)
Smallest Difference Description Given a number of distinct decimal digits, you can form one integer ...
- cookie、localStorage和sessionStorage三者的异同
1.三者都是用来存储浏览器数据的 2.生命周期:cookie可以设置数据失效时间,默认是关闭浏览器后失效:localStorage中的数据是永久保存的,除非手动清除:sessionStorage的数据 ...
- python gdal库安装
yum安装了postgis之后,会安装依赖gdal centos7.5的repo中gdal为1.11.4-3版本
- struct init
#include <stdio.h> #include <stdlib.h> #include <string.h> #include <stdbool.h& ...