How to implement custom paging in the DataGrid control _ self-study process

Source: Internet
Author: User
Tags bind
How do I implement a custom paging in a DataGrid control
In general, each time the DataGrid control implements a paging operation, the data in the data source is called again, and when the data
When there is a lot of data, this can be a waste of system resources and slow down the execution of programs. This time we usually solve by customizing pagination
Decide the problem.
The AllowCustomPaging property of the DataGrid control is used to get or set whether a DataGrid control allows custom points
Page The Virtualitemcoun property is used to get or set the actual number of items in the DataGrid when using custom paging. To implement a custom score
page, you must set both the AllowPaging and the AllowCustomPaging property to True.
The key to implementing a custom paging in the DataGrid is to make the control call only the data source data that is currently displayed, and the next
In the example of the CurrentPageIndex and PageSize properties, only the data required by the current page is taken at data binding.
(1) Page code:
Copy Code code 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= "Song Body" >
<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 Code code 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 description of Main.
</summary>
public class Main:System.Web.UI.Page
{
protected System.Web.UI.WebControls.DataGrid DataGrid1;
int startIndex = 0;//used to hold the starting index of the current page data item
private void Page_Load (object sender, System.EventArgs e)
{
if (! IsPostBack)
{
Bind ();//Data binding when initializing
}

}
void bind ()//Bound data method
{//Define database connection objects
SqlConnection cn=new SqlConnection ("server=.; Database=pubs;uid=sa;pwd= ");
Create a data adapter object
SqlDataAdapter Da=new SqlDataAdapter ("select title_id, title, type, pub_id
, price,pubdate from titles ", CN);
Creating a DataSet Object
DataSet ds=new DataSet ();
Try
{//PageSize records are taken from the specified index.
Da. Fill (Ds,startindex,datagrid1.pagesize, "curdatatable");
Da. Fill (ds, "alldatatable");//Populate data collection
Set the number of items that the DataGrid control actually displays
Datagrid1.virtualitemcount=ds. tables["Alldatatable"]. Rows.Count;
Make data binding
Datagrid1.datasource=ds. tables["Curdatatable"];
Datagrid1.databind ();

}
Catch
{
Page.registerclientscriptblock ("", "<script>alert") (' Data display error
False ');</script> ");
}

}

Web Forms Designer generated Code #region Web Forms Designer generated code
Override protected void OnInit (EventArgs e)
{
//
CodeGen: This call is required for the ASP.net Web forms Designer.
//
InitializeComponent ();
Base. OnInit (e);
}

/**////<summary>
Designer supports required methods-do not use the Code editor to modify
The contents of this method.
</summary>
private void InitializeComponent ()
{
This. Datagrid1.pageindexchanged + = new
System.Web.UI.WebControls.DataGridPageChangedEventHandler
(This. datagrid1_pageindexchanged);
This. Load + = new System.EventHandler (this. Page_Load);

}
#endregion

private void Datagrid1_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 user-selected page
Datagrid1.currentpageindex=e.newpageindex;
The total number of records so far for the current page, so that you can start reading from that record on the next page
Startindex=datagrid1.pagesize*datagrid1.currentpageindex;
Get bound data
Bind ();
}
}
}

In this program, the Virtualitemcoun property value of the DataGrid control is first set at the time of the data binding, as in the query result set.
The total number of records, and then the data to be displayed on the current page, when initialized, the current page displays data that starts with the 0-bit of the data being fetched, to
The number of data records until the PageSize property of the DataGrid control is set. To redefine the next page of data in a paging operation
The start index value of the data item, and then call the data-binding method to rebind the new data that is fetched and the DataGrid control.

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.