To add a page to the DataGrid Windows Control
When youDataGridWhen paging is performed, the data is displayed in the "Block" of the page size, that is, one page of records is displayed at a time. The sample code to followDatarowObject from memoryDatasetCopy to a temporary table. The temporary table subsequently correspondsDataGridControl binding.
1. |
Open a new Visual C #. Net windows application project. |
2. |
AddDataGridControlReadonlySet propertyTrue. |
3. |
Place the following additional controls on form1 and set their properties as follows:
Widget |
Name attribute |
Text attributes |
Button |
BtnFirstPage |
First Page |
Button |
BtnNextPage |
Next Page |
TextBox |
TxtDisplayPageNo |
|
Button |
BtnPreviousPage |
Previous Page |
Button |
BtnLastPage |
Last Page |
TextBox |
TxtPageSize |
5 |
Button |
BtnFillGrid |
Fill Grid |
DataGrid |
DataGrid1 |
|
|
4. |
Copy the following code and paste it to the top of the form1 code window. Make sure that each namespace is referenced only once. By default, it may have been referencedSystemAndSystem. Data.using System; using System.Data; using System.Data.SqlClient;
|
5. |
Copy the following code and paste it toTopTo declare form-level variables for form1:SqlDataAdapter da; DataSet ds; DataTable dtSource; int PageCount; int maxRec; int pageSize; int currentPage; int recNo;
|
6. |
Copy the following code and paste it to the staticMainAfter the method, the function scope is form-level:private void LoadPage() { int i; int startRec; int endRec; DataTable dtTemp; //Clone the source table to create a temporary table. dtTemp = dtSource.Clone(); if (currentPage == PageCount) { endRec = maxRec; } else { endRec = pageSize * currentPage; } startRec = recNo; //Copy rows from the source table to fill the temporary table. for (i = startRec; i < endRec; i++) { dtTemp.ImportRow(dtSource.Rows[i]); recNo += 1; } dataGrid1.DataSource = dtTemp; DisplayPageInfo(); } private void DisplayPageInfo() { txtDisplayPageNo.Text = "Page " + currentPage.ToString() + "/ " + PageCount.ToString(); } private bool CheckFillButton() { // Check if the user clicks the "Fill Grid" button. if (pageSize == 0) { MessageBox.Show("Set the Page Size, and then click the Fill Grid button!"); return false; } else { return true; } }
|
7. |
Paste the following codeForm1_loadDuring the event: //Open Connection. SqlConnection conn = new SqlConnection("Server=server;uid=login;pwd=pwd;database=northwind"); //Set the DataAdapter's query. da = new SqlDataAdapter("select * from customers", conn); ds = new DataSet(); //Fill the DataSet. da.Fill(ds, "customers"); //Set the source table. dtSource = ds.Tables["customers"];
|
8. |
Modify the connection string in the above Code to suit your environment:SqlConnection conn = new SqlConnection("Server=server;uid=login;pwd=pwd;database=northwind");
|
9. |
Double-clickFill GridTo open the btnFillGrid code window. Copy the following code and paste itBtnfillgrid_clickDuring the event: // Set the start and max records. pageSize = Convert.ToInt32(txtPageSize.Text); maxRec = dtSource.Rows.Count; PageCount = maxRec / pageSize; //Adjust the page number if the last page contains a partial page. if ((maxRec % pageSize) > 0) { PageCount += 1; } // Initial seeings currentPage = 1; recNo = 0; // Display the content of the current page. LoadPage();
|
10. |
Double-clickFirst pageTo open the btnFirstPage code window. Copy the following code and paste itBtnfirstpage_clickDuring the event: if (CheckFillButton() == false) { return; } //Check if you are already at the first page. if (currentPage == 1) { MessageBox.Show("You are at the First Page!"); return; } currentPage = 1; recNo = 0; LoadPage();
|
11. |
Double-clickNext PageTo open the btnNextPage code window. Copy the following code and paste itBtnnextpage_clickDuring the event: //If the user did not click the "Fill Grid" button, then return. if (CheckFillButton() == false) { return; } //Check if the user clicks the "Fill Grid" button. if (pageSize == 0) { MessageBox.Show("Set the Page Size, and then click the Fill Grid button!"); return; } currentPage += 1; if (currentPage > PageCount) { currentPage = PageCount; //Check if you are already at the last page. if (recNo == maxRec) { MessageBox.Show("You are at the Last Page!"); return; } } LoadPage();
|
12. |
Double-clickPrevious PageTo open the btnPreviousPage code window. Copy the following code and paste itBtnpreviouspage_clickDuring the event: if (CheckFillButton() == false) { return; } if (currentPage == PageCount) { recNo = pageSize * (currentPage - 2); } currentPage -= 1; //Check if you are already at the first page. if (currentPage < 1) { MessageBox.Show("You are at the First Page!"); currentPage = 1; return; } else { recNo = pageSize * (currentPage - 1); } LoadPage();
|
13. |
Double-clickLast pageTo open the btnLastPage code window. Copy the following code and paste itBtnlastpage_clickDuring the event: if (CheckFillButton() == false) { return; } //Check if you are already at the last page. if (recNo == maxRec) { MessageBox.Show("You are at the Last Page!"); return; } currentPage = PageCount; recNo = pageSize * (currentPage - 1); LoadPage();
|
14. |
Press F5 to generate and run this project. |
15. |
By default, Page Size (Page Size) is set to 5 Records. You can change this setting in the text box. |
16. |
ClickFill Grid. Note,DataGridFive records are entered. |
17. |
ClickFirst page,Next Page,Previous PageOrLast pageYou can view the page back and forth. |
Troubleshooting
• |
This method only applies to read-onlyDataGridControl. When you sendDatatableWhen an object is imported into a row, this is only a copy, and your changes are not saved to the master table. |
• |
If you wantDataRelationYou cannot use this method (or use a collection or array) If your records are linked to a parent-child relationship and appear on the form at the same time ). |