WinForm data source paging technology, winform source Paging

Source: Internet
Author: User

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 );
}
}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.