WinForm data source paging technology, winform source Paging
1. Compile the paging storage process
USE [Contacts]
GO
Create procedure [dbo]. [GetPageData]
(@ StartIndex int,
@ EndIndex int
)
As
Begin
With temptbl (
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. Design a form
3. write code
Public partial class FormPaging: Form
{
// Number of records displayed on each page
Int pageSize = 3;
// Current page number
Int page = 1;
Public FormPaging ()
{
InitializeComponent ();
}
// Obtain the total number of records
Int GetRecordCount ()
{
String SQL = "select count (*) from contact ";
Return Convert. ToInt32 (SqlDbHelper. ExecuteScalar (SQL ));
}
Void Fill (int page)
{
String SQL = "GetPageData"; // name of the paging Stored Procedure
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 );
}
// Fill the drop-down box with the number of pages
Private void BindCombox ()
{
Int total = GetRecordCount ();
// Calculate the total number of pages
Int totalPage = total % pageSize = 0? Total/pageSize: total/pageSize + 1;
For (int I = 1; I <= totalPage; I ++)
{
ComboBox1.Items. Add (I );
}
}
// Bind the DataGridView Control Based on the selected current page number.
Private void combobox#selectedindexchanged (object sender, EventArgs e)
{
Page = Convert. ToInt32 (comboBox1.Text );
Fill (page );
}
}