WinForm DataGridView分頁功能

2022-02-15 05:51:45 字數 4694 閱讀 9014

winform 裡面的datagridview不像webform裡面的gridview那樣有自帶的分頁功能,需要自己寫**來實現分頁,效果如下圖:

.cs:

using

system;

using

system.collections.generic;

using

system.componentmodel;

using

system.data;

using

system.drawing;

using

system.linq;

using

system.text;

using

system.windows.forms;

using

system.data.sqlclient;

namespace

allenpagetest

private

void form2_load(object

sender, eventargs e)

//總記錄數

public

int recordcount = 0

;

private

string strconn = @"

server=.;database=test;uid=sa;pwd=1234

";

private

string strprocedure = "

pagetest ";

//////

繫結第index頁的資料

/// ///

private

void binddatawithpage(int

index)

//////

獲取資料源

/// ///

連線物件

///儲存過程名稱

///頁碼 ///

每一頁顯示的行數

///private datatable getdata(string conn, string strprocedure, int pageindex, int

pagesize)

try

catch

(sqlexception err)

finally}}

private

void allenpage1_pageindexchanged(object

sender, eventargs e)}}

儲存過程:

if exists(select * from sysobjects where name='

pagetest')

drop proc pagetest

gocreate procedure [dbo].[pagetest]

@table varchar(

1000), --表名,多表是請使用 ta a inner join tb b on a.aid =b.aid

@tindex nvarchar(

100), --主鍵

@column nvarchar(

2000) = '

*',--要查詢的字段,全部欄位就為*@sql nvarchar(

3000) = '',--where條件

@pageindex int = 1, --開始頁碼

@pagesize int = 10, --每頁查詢資料的行數

@sort nvarchar(

200) = '' --排序的字段

asdeclare @strwhere varchar(

2000

)declare @strsql nvarchar(

3900

)if @sql is not null and len(ltrim(rtrim(@sql)))>0

begin

set @strwhere = '

where

' + @sql + '

'end

else

begin

set @strwhere = ''

end

if (charindex(ltrim(rtrim(@tindex)),@sort)=0

)begin

if(@sort=''

) set @sort = @tindex + '

desc

'else

set @sort = @sort+ '

, '+@tindex + '

desc

'end

if @pageindex

< 1

set @pageindex = 1

if @pageindex = 1

begin

set @strsql = '

select top

' + str(@pagesize) +'

'+@column+ '

from

' + @table + '

' + @strwhere + '

order by

'+@sort

end

else

begin

declare @start_id nvarchar(50)

declare @end_id nvarchar(50)

set @start_id = convert(nvarchar(50),(@pageindex - 1) * @pagesize + 1

) set @end_id = convert(nvarchar(50),@pageindex *@pagesize)

set @strsql = '

select

'+@column+ '

from (select row_number() over(order by '

+@sort+

') as rownum,

'+@column+

'from

'+@table +

' with(nolock) '

+ @strwhere +

') as d

where rownum between

'+@start_id+

' and '

+@end_id +

' order by '

+@sort

endexec(@strsql)

print @strsql

set @strsql = '

select count(1) as totalrecords from

' + @table +'

with(nolock)

' +@strwhere

print @strsql

exec(@strsql)

另一種儲存過程寫法:

create procedure pro_datapagerownumber

@sql nvarchar(

2000),--主句

@order nvarchar(

20),--排序

@pageindex

int,--當前頁

@pagesize

int,--每頁顯示數

@totalrow

int output--記錄總數

as set nocount on;

declare @excesql nvarchar(

4000)--主句

declare @startrow

asint--開始行

set @startrow=(@pageindex-1)*@pagesize+1

declare @lastrow

int--結束行

set @lastrow=@pagesize*@pageindex

declare @rownumber nvarchar(

100)

set @rownumber='

,row_number() over(order by

'+@order+'

) as rownumber from

'set @sql=replace(@sql,'

from

',@rownumber)

set @excesql='

select @totalrow=max(rownumber) from (

'+@sql+'

) as tmp

'execute sp_executesql @excesql,n

'@totalrow in output

',@totalrow output

set @excesql='

select * from(

'+@sql+'

) as tmp where rownumber between

'+convert(nvarchar,@startrow)

+'and

'+convert(nvarchar,@lastrow)

execute(@excesql)

go

Winform DataGridView初始化問題

一 初始化dgv屬性 通過 初始化dgv屬性,更便於檢視。datagridview.datasource null 1 禁止自動建立列 datagridview.autogeneratecolumns false 2 禁止使用者新增 刪除行 datagridview.allowusertoaddro...

vue element echarts 換膚功能

demo中的換膚效果沒有用到css預編譯處理 整體的思路是 預先定義好多套css樣式表分別放到static檔案中,根據按鈕切換在dom上掛載相對應的css,聽上去是個比較笨的方法,下面直接上 2.將預設的主題放在vuex中 import vue from vue import vuex from v...

ios xmpp openfire實現聊天功能

ios使用xmppframework 來實現基本的聊天功能 1.使用xmppstream連線openfire 需要賬戶,密碼和伺服器位址埠一般為5222 bool connect if userid nil pass nil 設定使用者 xmppstream setmyjid xmppjid jid...