admin 管理员组

文章数量: 887021

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。 本文链接: https://blog.csdn/david_520042/article/details/50675645

添加两个组件:BindingNavigator和BindingSource




代码:


   
   
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Data.SqlClient;
  6. using System.Drawing;
  7. using System.Text;
  8. using System.Linq;
  9. using System.Threading.Tasks;
  10. using System.Windows.Forms;
  11. using DevExpress.XtraEditors;
  12. using DZAMS.DBUtility;
  13. namespace DZAMS.Demo
  14. {
  15. public partial class GridPage_Frm : DevExpress. XtraEditors. XtraForm
  16. {
  17. public DataTable dt = new DataTable();
  18. StoreProcedure sp;
  19. private int pageSize = 10; //每页显示行数
  20. private int nMax = 0; //总记录数
  21. private int pageCount = 0; //页数=总记录数/每页显示行数
  22. private int pageCurrent = 0; //当前页号
  23. private DataSet ds = new DataSet();
  24. private DataTable dtInfo = new DataTable();
  25. public GridPage_Frm()
  26. {
  27. InitializeComponent();
  28. }
  29. private void GridPage_Frm_Load(object sender, EventArgs e)
  30. {
  31. string strQuery = string.Format( "SELECT Id, UserCode, UserName, RoleName, Ip, Mac, LoginTime FROM DZ_LoginLog");
  32. dt = SqlHelper.ExecuteDataset(SqlHelper.conn, CommandType.Text, strQuery.ToString()).Tables[ 0];
  33. gridControl1.DataSource = dt;
  34. string strConn = "SERVER=(local);DATABASE=DZ;UID=sa;PWD=XXXX"; //数据库连接字符串
  35. SqlConnection conn = new SqlConnection(strConn);
  36. conn.Open();
  37. string strSql = "SELECT count(*) as num FROM DZ_LoginLog";
  38. SqlDataAdapter sda = new SqlDataAdapter(strSql, conn);
  39. sda.Fill(ds, "ds");
  40. conn.Close();
  41. nMax = Convert.ToInt32(ds.Tables[ 0].Rows[ 0][ "num"].ToString());
  42. lblTotalCount.Text = nMax.ToString();
  43. lblPageSize.Text = pageSize.ToString();
  44. sp = new StoreProcedure( "Pr_Monitor_Pagination", strConn);
  45. dtInfo = sp.ExecuteDataTable( "DZ_LoginLog", "Id", "Id desc", pageCurrent++, pageSize);
  46. InitDataSet();
  47. }
  48. private void InitDataSet()
  49. {
  50. pageCount = (nMax / pageSize); //计算出总页数
  51. if ((nMax % pageSize) > 0) pageCount++;
  52. pageCurrent = 1; //当前页数从1开始
  53. LoadData();
  54. }
  55. private void LoadData()
  56. {
  57. lblPageCount.Text = "/"+pageCount.ToString();
  58. txtCurrentPage.Text = Convert.ToString(pageCurrent);
  59. this.bdsInfo.DataSource = dtInfo;
  60. this.bdnInfo.BindingSource = bdsInfo;
  61. this.gridControl1.DataSource = bdsInfo;
  62. }
  63. private void bdnInfo_ItemClicked(object sender, ToolStripItemClickedEventArgs e)
  64. {
  65. if (e.ClickedItem.Text == "导出当前页")
  66. {
  67. SaveFileDialog saveFileDialog = new SaveFileDialog();
  68. saveFileDialog.Title = "导出Excel";
  69. saveFileDialog.Filter = "Excel文件(*.xls)|*.xls";
  70. DialogResult dialogResult = saveFileDialog.ShowDialog( this);
  71. if (dialogResult == DialogResult.OK)
  72. {
  73. DevExpress.XtraPrinting.XlsExportOptions options = new DevExpress.XtraPrinting.XlsExportOptions();
  74. gridControl1.ExportToXls(saveFileDialog.FileName, options);
  75. // gridControl1.ExportToExcelOld(saveFileDialog.FileName);
  76. DevExpress.XtraEditors.XtraMessageBox.Show( "保存成功!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
  77. }
  78. }
  79. if (e.ClickedItem.Text == "关闭")
  80. {
  81. this.Close();
  82. }
  83. if (e.ClickedItem.Text == "首页")
  84. {
  85. pageCurrent--;
  86. if (pageCurrent <= 0)
  87. {
  88. MessageBox.Show( "已经是首页,请点击“下一页”查看!");
  89. return;
  90. }
  91. else
  92. {
  93. pageCurrent = 1;
  94. dtInfo = sp.ExecuteDataTable( "DZ_LoginLog", "Id", "Id desc", pageCurrent, pageSize);
  95. }
  96. }
  97. if (e.ClickedItem.Text == "上一页")
  98. {
  99. pageCurrent--;
  100. if (pageCurrent <= 0)
  101. {
  102. MessageBox.Show( "已经是第一页,请点击“下一页”查看!");
  103. return;
  104. }
  105. else
  106. {
  107. dtInfo = sp.ExecuteDataTable( "DZ_LoginLog", "Id", "Id desc", pageCurrent, pageSize);
  108. }
  109. }
  110. if (e.ClickedItem.Text == "下一页")
  111. {
  112. pageCurrent++;
  113. if (pageCurrent > pageCount)
  114. {
  115. MessageBox.Show( "已经是最后一页,请点击“上一页”查看!");
  116. return;
  117. }
  118. else
  119. {
  120. dtInfo = sp.ExecuteDataTable( "DZ_LoginLog", "Id", "Id desc", pageCurrent, pageSize);
  121. }
  122. }
  123. if (e.ClickedItem.Text == "尾页")
  124. {
  125. pageCurrent++;
  126. if (pageCurrent > pageCount)
  127. {
  128. MessageBox.Show( "已经是尾页,请点击“上一页”查看!");
  129. return;
  130. }
  131. else
  132. {
  133. pageCurrent = pageCount;
  134. dtInfo = sp.ExecuteDataTable( "DZ_LoginLog", "Id", "Id desc", pageCount, pageSize);
  135. }
  136. }
  137. LoadData();
  138. }
  139. }
  140. }

StoreProcedure类:


   
   
  1. public class StoreProcedure
  2. {
  3. // 存储过程名称。
  4. private string _name;
  5. // 数据库连接字符串。
  6. private string _conStr;
  7. // 构造函数
  8. // sprocName: 存储过程名称;
  9. // conStr: 数据库连接字符串。
  10. public StoreProcedure(string sprocName, string conStr)
  11. {
  12. _conStr = conStr;
  13. _name = sprocName;
  14. }
  15. // 执行存储过程,不返回值。
  16. // paraValues: 参数值列表。
  17. // return: void
  18. public void ExecuteNoQuery(params object[] paraValues)
  19. {
  20. using (SqlConnection con = new SqlConnection(_conStr))
  21. {
  22. SqlCommand comm = new SqlCommand(_name, con);
  23. comm.CommandType = CommandType.StoredProcedure;
  24. AddInParaValues(comm, paraValues);
  25. con.Open();
  26. comm.ExecuteNonQuery();
  27. con.Close();
  28. }
  29. }
  30. // 执行存储过程返回一个表。
  31. // paraValues: 参数值列表。
  32. // return: DataTable
  33. public DataTable ExecuteDataTable(params object[] paraValues)
  34. {
  35. SqlCommand comm = new SqlCommand(_name, new SqlConnection(_conStr));
  36. comm.CommandType = CommandType.StoredProcedure;
  37. AddInParaValues(comm, paraValues);
  38. SqlDataAdapter sda = new SqlDataAdapter(comm);
  39. DataTable dt = new DataTable();
  40. sda.Fill(dt);
  41. return dt;
  42. }
  43. // 执行存储过程,返回SqlDataReader对象,
  44. // 在SqlDataReader对象关闭的同时,数据库连接自动关闭。
  45. // paraValues: 要传递给给存储过程的参数值类表。
  46. // return: SqlDataReader
  47. public SqlDataReader ExecuteDataReader(params object[] paraValues)
  48. {
  49. SqlConnection con = new SqlConnection(_conStr);
  50. SqlCommand comm = new SqlCommand(_name, con);
  51. comm.CommandType = CommandType.StoredProcedure;
  52. AddInParaValues(comm, paraValues);
  53. con.Open();
  54. return comm.ExecuteReader(CommandBehavior.CloseConnection);
  55. }
  56. // 获取存储过程的参数列表。
  57. private ArrayList GetParas()
  58. {
  59. SqlCommand comm = new SqlCommand( "dbo.sp_sproc_columns_90",
  60. new SqlConnection(_conStr));
  61. comm.CommandType = CommandType.StoredProcedure;
  62. comm.Parameters.AddWithValue( "@procedure_name", ( object)_name);
  63. SqlDataAdapter sda = new SqlDataAdapter(comm);
  64. DataTable dt = new DataTable();
  65. sda.Fill(dt);
  66. ArrayList al = new ArrayList();
  67. for ( int i = 0; i < dt.Rows.Count; i++)
  68. {
  69. al.Add(dt.Rows[i][ 3].ToString());
  70. }
  71. return al;
  72. }
  73. // 为 SqlCommand 添加参数及赋值。
  74. private void AddInParaValues(SqlCommand comm, params object[] paraValues)
  75. {
  76. comm.Parameters.Add( new SqlParameter( "@RETURN_VALUE", SqlDbType.Int));
  77. comm.Parameters[ "@RETURN_VALUE"].Direction =
  78. ParameterDirection.ReturnValue;
  79. if (paraValues != null)
  80. {
  81. ArrayList al = GetParas();
  82. for ( int i = 0; i < paraValues.Length; i++)
  83. {
  84. comm.Parameters.AddWithValue(al[i + 1].ToString(),
  85. paraValues[i]);
  86. }
  87. }
  88. }
  89. }

存储过程:


   
   
  1. ALTER procedure [dbo].[Pr_Monitor_Pagination]
  2. -- ============================================= == Paging == =============================================
  3. --Author: Lee
  4. --Create date: 2010\06\11
  5. --Parameter:
  6. -- 1.Tables :The Name Of Table or view
  7. -- 2.PrimaryKey :Primary Key
  8. -- 3.Sort :Ordering Statement,Without Order By, For Example:NewsID Desc,OrderRows Asc
  9. -- 4.CurrentPage :The Page Number Of Current page
  10. -- 5.PageSize :The Size Of One Page's Group
  11. -- 6.Fields :The Field Of You Needed
  12. -- 7.Filter :Where Condition,Without Where
  13. -- 8.Group :Group Condition,Without Group By
  14. -- 9.GetCount :Return The Number Of All, Not Zero
  15. --Updates:
  16. -- 2010\06\09 Create Procedure.
  17. -- ========================================================================================================
  18. @ Tables varchar( 600),
  19. @PrimaryKey varchar( 100),
  20. @ Sort varchar( 200)= null,
  21. @CurrentPage bigint= 1,
  22. @PageSize bigint= 10,
  23. @ Fields varchar( 1000)= '*',
  24. @Filter varchar( 1000)= null,
  25. @ Group varchar( 1000)= null,
  26. @GetCount bit= 0
  27. as
  28. if(@GetCount= 0)
  29. begin
  30. /*Ordering Of Default */
  31. if @ Sort is null or @ Sort= ''
  32. set @ Sort=@PrimaryKey
  33. declare @SortTable varchar( 100)
  34. declare @SortName varchar( 100)
  35. declare @strSortColumn varchar( 200)
  36. declare @ operator char( 2)
  37. declare @ type varchar( 100)
  38. declare @prec int
  39. /*Setting Condition Of Ordering*/
  40. if charindex( 'desc',@ Sort)> 0
  41. begin
  42. set @strSortColumn= replace(@ Sort, 'desc', '')
  43. set @ operator= '<='
  44. end
  45. else
  46. begin
  47. if charindex( 'asc',@ Sort)= 0
  48. set @strSortColumn= replace(@ Sort, 'asc', '')
  49. set @ operator= '>='
  50. end
  51. if charindex( '.',@strSortColumn)> 0
  52. begin
  53. set @SortTable= substring(@strSortColumn, 0, charindex( '.',@strSortColumn))
  54. set @SortName= substring(@strSortColumn, charindex( '.',@strSortColumn)+ 1, len(@strSortColumn))
  55. end
  56. else
  57. begin
  58. set @SortTable=@ Tables
  59. set @SortName=@strSortColumn
  60. end
  61. 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
  62. if charindex( 'char',@ type)> 0
  63. set @ type=@ type+ '('+ cast(@prec as varchar)+ ')'
  64. declare @strPageSize varchar( 50)
  65. declare @strStartRow varchar( 50)
  66. declare @strFilter varchar( 1000)
  67. declare @strSimpleFilter varchar( 1000)
  68. declare @strGroup varchar( 1000)
  69. /*CurrentPage Of Default*/
  70. if @CurrentPage< 1
  71. set @CurrentPage= 1
  72. /*Setting Paging param*/
  73. set @strPageSize= cast(@PageSize as varchar( 50))
  74. set @strStartRow= cast(((@CurrentPage -1)*@PageSize+ 1) as varchar( 50))
  75. /*Condition Of Filter And Group*/
  76. if @Filter is not null and @Filter!= ''
  77. begin
  78. set @strFilter= ' where '+@Filter+ ' '
  79. set @strSimpleFilter= ' and '+@Filter + ' '
  80. end
  81. else
  82. begin
  83. set @strSimpleFilter= ''
  84. set @strFilter= ''
  85. end
  86. if @ Group is not null and @ Group!= ''
  87. set @strGroup= ' group by '+@ Group+ ' '
  88. else
  89. set @strGroup= ''
  90. 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 + ' ')
  91. end
  92. else
  93. begin
  94. declare @strSQL varchar( 5000)
  95. if @Filter != ''
  96. set @strSQL = 'select count(' + @PrimaryKey + ') as Total from [' + @ Tables + '] where ' + @Filter
  97. else
  98. set @strSQL = 'select count(' + @PrimaryKey + ') as Total from [' + @ Tables + ']'
  99. exec(@strSQL)
  100. end

效果:


本文标签: 分页 DevExpress GridControl