日韩av黄I国产麻豆传媒I国产91av视频在线观看I日韩一区二区三区在线看I美女国产在线I麻豆视频国产在线观看I成人黄色短片

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 >

WinForm数据源分页技术

發(fā)布時間:2025/4/14 34 豆豆
生活随笔 收集整理的這篇文章主要介紹了 WinForm数据源分页技术 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

1、編寫分頁存儲過程

USE [Contacts]
GO

create procedure [dbo].[GetPageData]
(@startIndex int,
@endIndex int
)
as
begin
with temptbl as (
SELECT ROW_NUMBER() OVER (ORDER BY contact.id )AS Row, contact.id,name,phone,email,groupname from contact inner join contactgroup on contact.groupid=contactgroup.id )
SELECT * FROM temptbl where Row between @startIndex and @endIndex
end
GO

2、設(shè)計窗體

3、編寫代碼

public partial class FormPaging : Form
{

//每頁顯示的記錄數(shù)
int pageSize = 3;

//當前頁碼
int page=1;

public FormPaging()
{
InitializeComponent();
}

//獲取總的記錄數(shù)
int GetRecordCount()
{
string sql = "select count(*) from contact";
return Convert.ToInt32(SqlDbHelper.ExecuteScalar(sql));
}
void Fill(int page)
{
string sql = "GetPageData";//分頁存儲過程名稱
int startIndex = (page - 1) * pageSize + 1;
int endIndex = page * pageSize;
SqlParameter[] sp ={
new SqlParameter("@startIndex",startIndex),
new SqlParameter("@endIndex", endIndex)
};
DataTable dt = SqlDbHelper.ExecuteDataTable(sql, CommandType.StoredProcedure, sp);
dgvContactList.DataSource = dt;
}
private void FormPaging_Load(object sender, EventArgs e)
{
BindCombox();
Fill(page);
}

//用頁數(shù)填充下拉框

private void BindCombox()
{
int total = GetRecordCount();

//計算總的頁數(shù)
int totalPage = total % pageSize == 0 ? total / pageSize : total / pageSize + 1;
for (int i = 1; i <=totalPage; i++)
{
comboBox1.Items.Add(i);
}
}

?//根據(jù)用戶選擇的當前頁碼,綁定DataGridView控件

private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
page = Convert.ToInt32(comboBox1.Text);
Fill(page);
}
}

轉(zhuǎn)載于:https://www.cnblogs.com/zhouhb/p/5171152.html

總結(jié)

以上是生活随笔為你收集整理的WinForm数据源分页技术的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。