WinForm DataGridView分页功能
2020-12-13 05:56
标签:des winform datagridview 分页控件 blog http 使用 os WinForm 里面的DataGridView不像WebForm里面的GridView那样有自带的分页功能,需要自己写代码来实现分页,效果如下图: 分页控件 .CS: 存储过程: 还有一种存储过程写法,仅供参考: 分页的控件是自己写的一个用户控件,生成之后是一个DLL文件,直接引用在项目里面即可,有需要的可以留下邮箱。 WinForm DataGridView分页功能,搜素材,soscw.com WinForm DataGridView分页功能 标签:des winform datagridview 分页控件 blog http 使用 os 原文地址:http://www.cnblogs.com/host-2008/p/3890542.html 1 using System;
2 using System.Collections.Generic;
3 using System.ComponentModel;
4 using System.Data;
5 using System.Drawing;
6 using System.Linq;
7 using System.Text;
8 using System.Windows.Forms;
9 using System.Data.SqlClient;
10
11 namespace allenPageTest
12 {
13 public partial class Form2 : Form
14 {
15 public Form2()
16 {
17 InitializeComponent();
18 }
19
20 private void Form2_Load(object sender, EventArgs e)
21 {
22 BindDataWithPage(1);
23 }
24
25 //总记录数
26 public int RecordCount = 0;
27 private string strConn = @"server=.;database=test;uid=sa;pwd=1234";
28 private string strProcedure = "PageTest ";
29
30
31 ///
1 IF EXISTS(SELECT * FROM SYSOBJECTS WHERE NAME=‘PageTest‘)
2 DROP PROC PageTest
3 GO
4 CREATE PROCEDURE [dbo].[PageTest]
5 @Table VARCHAR(1000), --表名,多表是请使用 tA a inner join tB b On a.AID = b.AID
6 @TIndex NVARCHAR(100), --主键
7 @Column NVARCHAR(2000) = ‘*‘,--要查询的字段,全部字段就为*
8 @Sql NVARCHAR(3000) = ‘‘,--Where条件
9 @PageIndex INT = 1, --开始页码
10 @PageSize INT = 10, --每页查询数据的行数
11 @Sort NVARCHAR(200) = ‘‘ --排序的字段
12
13 AS
14
15
16
17 DECLARE @strWhere VARCHAR(2000)
18 DECLARE @strsql NVARCHAR(3900)
19 IF @Sql IS NOT NULL AND len(LTRIM(RTRIM(@Sql)))>0
20 BEGIN
21 SET @strWhere = ‘ WHERE ‘ + @Sql + ‘ ‘
22 END
23 ELSE
24 BEGIN
25 SET @strWhere = ‘‘
26 END
27
28 IF (charindex(LTRIM(RTRIM(@TIndex)),@Sort)=0)
29 BEGIN
30 IF(@Sort=‘‘)
31 SET @Sort = @TIndex + ‘ DESC ‘
32 ELSE
33 SET @Sort = @Sort+ ‘ , ‘+@TIndex + ‘ DESC ‘
34 END
35 IF @PageIndex
1 CREATE PROCEDURE pro_DataPageRowNumber
2 @SQL nvarchar(2000),--主句
3 @Order nvarchar(20),--排序
4 @PageIndex int,--当前页
5 @PageSize int,--每页显示数
6 @TotalRow int output--记录总数
7 AS
8 SET NOCOUNT ON;
9 declare @ExceSQL nvarchar(4000)--主句
10 declare @startRow as int--开始行
11 set @startRow=(@PageIndex-1)*@PageSize+1
12 declare @lastRow int--结束行
13 set @lastRow=@PageSize*@PageIndex
14 declare @RowNumber nvarchar(100)
15 set @RowNumber=‘,Row_NUMBER() OVER(ORDER BY ‘+@Order+‘) as RowNumber from ‘
16 set @SQL=Replace(@SQL,‘ from ‘,@RowNumber)
17 set @ExceSQL=‘select @TotalRow=max(RowNumber) from (‘+@SQL+‘) as tmp‘
18 execute sp_executesql @ExceSQl,N‘@TotalRow in output‘,@TotalRow output
19 set @ExceSQL=‘select * from(‘+@SQL+‘) as tmp where RowNumber between‘+ Convert(nvarchar,@startRow)
20 +‘ and ‘+Convert(nvarchar,@lastRow)
21 execute(@ExceSQL)
22
23 GO