For the code of the DataGrid page in winform, We need to display data one by one when we use the DataGrid. When a button is clicked, the next record appears, that is, only one record is displayed at a time ,~ So I came up with the idea of using DataGrid paging. Of course, we also encountered a lot of problems in development, especially in details ~ Here, I would like to thank my friends reficul and Oscar for helping me a lot ~ ------------------------- Code example Public int nowpage = 1, pagecount = 1, pagesize = 1; // first define the variable and initialize private void button#click (Object sender, system. eventargs e) // Previous Page
{
Nowpage --;
Loaddata (sender, e );
} Private void button2_click (Object sender, system. eventargs e) // next page
{
Nowpage ++;
Loaddata (sender, e );
}
Private void loaddata (Object sender, system. eventargs E)
{
Try
{
String conn = "Server =.; user id = sa; Password =; database = northwind"; // connect to the database. In this case, the database is in SQL. // set the database connection.
Sqlconnection sconnstr = new sqlconnection (conn );
If (sconnstr. state. tostring () = "closed ")
{
Sconnstr. open ();
} Sqlcommand countcmd = new sqlcommand ("select count (*) as Co from employees", sconnstr); // read the total number of records
Sqldatareader countdr = countcmd. executereader (); If (countdr. Read ())
{
Pagecount = int32.parse (countdr ["CO"]. tostring ()/pagesize; // use the total number of records except the number of records displayed on each page = total number of pages
If (int32.parse (countdr ["CO"]. tostring () % pagesize> 0) // modulo operation,
{
Pagecount = pagecount + 1; // The number of pages displayed on more than one page is calculated by two pages.
}
If (pagecount <1) // if the number of less than one page is displayed, the value is calculated by one page.
{
Pagecount = 1;
}
}
Sconnstr. Close ();
Pageinfo. Text = "Total" + pagecount + "Page \ t No." + nowpage + "page ";
// Set database operation commands
If (nowpage> = pagecount) // if the current page value is greater than or equal to the total number of pages, the current page is the last page
{
Nowpage = pagecount;
}
If (nowpage <= 1) // if the current page value is less than or equal to one page, the current page is the first page
{
Nowpage = 1;
} Int start = (NowPage-1) * pagesize; // The start record of the current page is the last record of the total number of previous pages string command = "select * from employees "; // fill in the data and display it
Sqldataadapter SDA = new sqldataadapter (command, sconnstr );
Dataset DS = new dataset ();
SDA. Fill (DS, start, pagesize, "employees ");
Datagrid1.datasource = Ds. Tables ["employees"]. defaultview;
Datagrid1.setdatabinding (DS, "employees ");
}
Catch (exception ERR)
{
MessageBox. Show (ERR. Message. tostring ());
}
} Private void button3_click (Object sender, system. eventargs e) // homepage {
Nowpage = 1;
Loaddata (sender, e );
} Private void button4_click (Object sender, system. eventargs e) // the last page.
{
Nowpage = pagecount;
Loaddata (sender, e );
}
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