HOW TO: Using Visual C #

xiaoxiao2021-03-06  41

HOW TO: Use Visual C # .NET to execute paging on the DataGrid Windows control

Article ID: 307710 Last Update Date: June 21, 2002 Version: 1.0

The release number of this article has been CHS307710

For Microsoft Visual Basic .NET versions of this article, see

305271.

For Microsoft Visual J # .NET versions of this article, see

CHS320626.

This article references the following Microsoft .NET Framework Class Bank Name Space:

• System.Data.Data • System.Data.sqlclient

This task content

•summary

• Requirements • Steps to add paging to the DataGrid Windows control • Troubleshooting • Reference

This page

Summary reference

summary

Data grid web control has built-in automatic or custom paging features, but

Data grid Windows controls do not have these features. This article describes how to

Data Grid Windows control creates a simple paging mechanism.

The code example in this article uses

Data set objects. In ADO.NET,

Data set objects are filled with a single operation, they always reside in memory. If you are using a large

Data sets, this article describes how to display data in a programming method or page form.

This example uses the Customers table in the Microsoft SQL Server Northwind database as the backend of the database. If you connect to any other database or a different table, make sure the code is updated accordingly.

This trick has some limitations. For more information, see

Troubleshooting section.

Back to top

Require the following list lists the recommended hardware, software, network structure, and service pack required:

• Microsoft Windows 2000 Professional, Windows 2000 Server, Windows 2000 Advanced Server or Windows NT 4.0 Server • Microsoft Visual Studio .NET • Microsoft SQL Server 7.0 or later This article assumes that you are familiar with the following topics:

• Visual C # .NET • ADO.NET Basics and Syntax

Back to top

Steps to add a pagination to the DataGrid Windows control when you

When DataGrid is panelively, you will display data in the "block area" of the page size, ie, a page record is displayed at a time. The following code example will be

DataRow object from memory

The dataset is copied into a temporary table. The temporary table will then be bound to

DataGrid control.

1. Open the new Visual C # .NET Windows application project. 2. Add a DataGrid control to set its readOrthly property to true. 3. Add the following additional controls to FORM1 and set their properties as shown below:

Text Attribute Name Attribute Control 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 and paste to the top of the code window of Form1 code below. Make sure that only references to each name space. System and System.Data may have been referenced by default. Using system;

Using system.data;

Using system.data.sqlclient; 5. Copy the following code and paste it into the top of the public form1 to declare the window-level variables for the 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 into the position next to the static empty main method so that its role range is the form level: private void loading () {

INT I;

Int StartRec;

Int end indec;

DataTable DTTEMP;

// Clone The Source Table To Create a TempoRable 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

DTTEMP.IMPORTROW (DTSource.Rows [i]);

Recno = 1;

}

DataGrid1.datasource = DTTEMP;

DisplayPageInfo ();

}

Private void displaypageInfo () {

TXTDISPLAYPAGENO.TEXT = "Page" CurrentPage.toTString () "/" PageCount.toString ();

}

Private bool checkfillbutton () {

// Check if the user copys the "Fill Grid" button.

IF (PageSize == 0) {

MessageBox.show ("SET the page size, and then aen click!");

Return False;

}

Else {

Return True;

}

} 7. Paste the following code to the Form1_Load event process: // Open connection.

SqlConnection Con = 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 that appears in the above code so that your environment: SQLConnection Con = New SqlConnection ("Server = Server; UID = Login; PWD = PWD; dataBase = northwind ");

Double-click Fill Grid to open the BTNFillGrid code window. Copy the code below and paste it into the btnfillgrid_click incident:

// set the start and max replands.

PageSize = Convert.Toint32 (TXTPAGESIZE.TEXT);

MaxRec = dtsource.rows.count;

PageCount = maxRec / pageSize;

// Adjust the page number if The last page contacts a partial page.

IF ((MaxRec% PageSize)> 0) {

PageCount = 1;

}

// Initial seeings

CurrentPage = 1;

Recno = 0;

// Display The Content of the Current Page.

LoadPage (); 9. Double-click First Page to open the BTNFirstPage's code window. Copy the following code and paste it into the BTnFirstPage_Click event process: 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 (); 10. Double-click Next Page to open the BTnNextPage's code window. Copy the following code and paste it into the btnnextPage_Click event process: // if the user did not click The "Fill Grid" Button, Then Return.

IF (CheckfillButton () == false) {

Return;

}

// Check if the user copys the "Fill Grid" button.

IF (PageSize == 0) {

MessageBox.show ("SET the page size, and then aen click!");

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 (); 11. Double-click Previous Page to open the BTNPReviouspage code window. Copy the following code and paste it into the BtnPReviouspage_Click event: if (checkfillbutton () == false {

Return;

}

IF (currentpage == pagecount) {

Recno = pagesize * (currentpage -2);

}

CurrentPage - = 1;

// Check if you are already at the first page.

CurrentPage <1) {

MessageBox.show ("You are at the first page!");

CurrentPage = 1;

Return;

}

Else {

Recno = Pagesize * (CurrentPage - 1);

}

LoadPage (); 12. Double-click Last Page to open the BTNLastPage's code window. Copy the following code and paste it into the BTNLASTPAGE_CLICK 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 (); 13. Press F5 to build and run this project. 14. By default, page size is set to 5 records. This setting can be changed in the text box. 15. Click Fill Grid. Note that 5 records were filled in the DataGrid (data grid). 16. Click First Page, Next Page, Previous Page and Last Page. You can browse between different pages.

Back to top

Troubleshooting

• This skill is only available to read-only DataGrid controls. When you import a row of data to the DataTable object, it is actually created a copy. Therefore, the changes will not be saved to the home table. • If you want users to locate subcords through a DataRelation object, or if your record is linked by a parent-child relationship and simultaneously appearing on the form, you cannot use this trick (you can't use a collection or array).

Back to top

reference

For more information about ADO.NET, please visit the following MSDN Web site:

Accessing Data with ADO.NET (access data using ADO.NET)

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpguide/html/cpconaccessingDatawithadonet.asp

For more information, see the Microsoft .NET Framework SDK document:

.NET frame SDK

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/netstart/html/sdkstart.asp Back to top

The information in this article applies to:

• Microsoft .NET Framework 1.1 Service Pack 1 • Microsoft Visual C # .NET 2002 Standard Edition

Back to top

Keywords: kbhowto kbhowtomaster kbsqlclient kbsystemdata KB307710

Back to top

Microsoft and / or its suppliers do not make any declarations for files published on this server and related to graphics for any purpose. All such files and related graphics are "basically" without any nature guarantee. Microsoft and / or its suppliers here state state statements that all guarantees related to these information is not responsible, including all the default guarantees for the preferred, compliant, ownership, and non-infringement And conditions. In any case, the Microsoft and / or its suppliers caused by loss, data or profits due to loss of use, data or profits due to the use of or running the information on this server or in operation. special,

转载请注明原文地址:https://www.9cbs.com/read-78160.html

New Post(0)