How to Implement custom paging in the DataGrid Control
In general, the DataGrid Control calls the data in the data source again each time it performs a page flip operation.
When there is a lot of data, this will waste system resources and reduce program execution efficiency. At this time, we generally use custom pages to solve the problem.
Solve this problem.
The AllowCustomPaging attribute of the DataGrid control is used to obtain or set whether the DataGrid control allows custom points.
Page. The VirtualItemCoun attribute is used to obtain or set the actual number of items in the DataGrid when custom pages are used.
The AllowPaging and AllowCustomPaging attributes must be set to "True ".
The key to implementing custom paging in the DataGrid is to enable the control to only call the data source currently displayed.
In the preceding example, only the data required for the current page is obtained when data is bound using the CurrentPageIndex and PageSize attributes.
(1) Page code:
Copy codeThe Code is as follows:
<% @ Page language = "c #" Codebehind = "Main. aspx. cs" AutoEventWireup = "false"
Inherits = "SissonDemo. Main" %>
<! Doctype html public "-// W3C // dtd html 4.0 Transitional // EN">
<HTML>
<HEAD>
<Title> Main </title>
<Meta name = "GENERATOR" Content = "Microsoft Visual Studio. NET 7.1">
<Meta name = "CODE_LANGUAGE" Content = "C #">
<Meta name = "vs_defaultClientScript" content = "JavaScript">
<Meta name = "vs_targetSchema"
Content = "http://schemas.microsoft.com/intellisense/ie5">
</HEAD>
<Body MS_POSITIONING = "GridLayout">
<Form id = "Form1" method = "post" runat = "server">
<FONT face = "">
<Asp: DataGrid id = "DataGrid1" style = "Z-INDEX: 101; LEFT: 8px; POSITION:
Absolute; TOP: 24px "runat =" server"
Width = "792px" Height = "96px" AllowCustomPaging = "True"
AllowPaging = "True" PageSize = "5">
<PagerStyle Mode = "NumericPages"> </PagerStyle>
</Asp: DataGrid> </FONT>
</Form>
</Body>
</HTML>
(2) Background code:
Copy codeThe Code is as follows:
Using System;
Using System. Collections;
Using System. ComponentModel;
Using System. Data;
Using System. Drawing;
Using System. Web;
Using System. Web. SessionState;
Using System. Web. UI;
Using System. Web. UI. WebControls;
Using System. Web. UI. HtmlControls;
Using System. Data. SqlClient;
Namespace SissonDemo
{
/** // <Summary>
/// Summary of Main.
/// </Summary>
Public class Main: System. Web. UI. Page
{
Protected System. Web. UI. WebControls. DataGrid DataGrid1;
Int startIndex = 0; // used to save the start index of the data item on the current page
Private void Page_Load (object sender, System. EventArgs e)
{
If (! IsPostBack)
{
Bind (); // Bind data during initialization
}
}
Void Bind () // Bind data method
{// Define the database connection object
SqlConnection cn = new SqlConnection ("server =.; database = pubs; uid = sa; pwd = ");
// Create a data adaptation object
SqlDataAdapter da = new SqlDataAdapter ("select title_id, title, type, pub_id
, Price, pubdate from titles ", cn );
// Create a DataSet object
DataSet ds = new DataSet ();
Try
{// Retrieve PageSize records from the specified index.
Da. Fill (ds, startIndex, DataGrid1.PageSize, "CurDataTable ");
Da. Fill (ds, "AllDataTable"); // Fill in the data set
// Set the number of items to be displayed in the DataGrid Control.
DataGrid1.VirtualItemCount = ds. Tables ["AllDataTable"]. Rows. Count;
// Bind data
DataGrid1.DataSource = ds. Tables ["CurDataTable"];
DataGrid1.DataBind ();
}
Catch
{
Page. RegisterClientScriptBlock ("", "<script> alert ('data Display Error
'); </Script> ");
}
}
Code generated by Web form designer # code generated by region Web Form Designer
Override protected void OnInit (EventArgs e)
{
//
// CODEGEN: This call is required by the ASP. NET Web form designer.
//
InitializeComponent ();
Base. OnInit (e );
}
/** // <Summary>
/// The designer supports the required methods-do not use the code editor to modify
/// Content of this method.
/// </Summary>
Private void InitializeComponent ()
{
This. Maid + = new
System. Web. UI. WebControls. DataGridPageChangedEventHandler
(This. Maid pageindexchanged );
This. Load + = new System. EventHandler (this. Page_Load );
}
# Endregion
Private void datagrid#pageindexchanged (object source,
System. Web. UI. WebControls. DataGridPageChangedEventArgs e)
{
// Set the index value of the current page of The DataGrid to the index of the page selected by the user
DataGrid1.CurrentPageIndex = e. NewPageIndex;
// Obtain the total number of records until the current page, so that the records can be read from the record on the next page.
StartIndex = maid * maid;
// Obtain the bound data
Bind ();
}
}
}
In this program, when binding data, set the VirtualItemCoun attribute value of the DataGrid Control
The total number of records, and then obtain the data to be displayed on the current page. during initialization, the data displayed on the current page starts from the zero position of the retrieved data
The number of data records until the set value of the PageSize attribute of the DataGrid Control. When you redefine data on the next page in a paging operation
And then call the data binding method to re-bind the new data and the DataGrid Control.