[Transfer] ASP.NET Custom Points of DataGrid Controls (Zheng Zuo 2004-10-28)

xiaoxiao2021-03-06  17

Although it is more convenient to use DataGrid, it is more convenient. Make the efficiency very low. The following is reduced by DataGrid's custom paging feature to reduce resource usage and improve efficiency.

The key to the implementation is to set the AllowCustomPaging property bit True and set the VirtualItemCount property to the total record number. Provide the page for the paging. The main code of the front desk is as follows:

Border = "1">

The data source used here is also assumed to be Northwind's Customers table.

Below is the stored procedure to access the single page, there are a lot of implementation, but this is the most common,

Create Procedure [getCustomersDataPage]

@PageIndex Int,

@PageSize Int,

@Recordcount Int Out,

@PageCount Int Out

AS

SELECT @Recordcount = count (*) from customer

Set @PageCount = CEILING (@Recordcount * 1.0 / @PageSize)

Declare @sqlstr nvarchar (1000)

IF @PageIndex = 0 or @PageCount <= 1

Set @Sqlstr = N'SELECT TOP ' STR (@PageSize)

'Customerid, CompanyName, Address, Phone from Customers Order by Customerid Desc'

Else if @PageIndex = @PageCount - 1

Set @Sqlstr = n 'SELECT * FROM (SELECT TOP' STR (@RecordCount - @PageSize * @PageIndex)

'Customerid, CompanyName, Address, Phone from Customers Order by Customerid ASC) Temptable Order by Customerid Desc'Else

Set @sqlstr = n 'SELECT TOP' STR (@PageSize) '* from (SELECT TOP' STR (@Recordcount - @PageSize * @PageIndex)

'Customerid, CompanyName, Address, Phone from Customers Order by Customerid ASC) Temptable Order By Customerid Desc'

EXEC (@SQLSTR)

Go

Gets the number of records and pages use the output parameters of the stored procedure.

Get the data source, here returns a DataSet.

First define a member,

PRIVATE INT pageCOUNT; / / Page

Private int recordcount; // record number

// Get a single page data

Private Static DataSet GetCustomersdata (int pageIndex, int pageSize, ref int cablecount)

{

String connString = configurationSettings.appsettings ["connString"];

SqlConnection conn = new sqlconnection (connString);

SQLCommand Comm = New Sqlcommand ("GetCustomersdataPage", CONN);

Comm.Parameters.Add (New Sqlparameter);

Comm.Parameters [0] .value = pageindex;

Comm.Parameters.Add (New Sqlparameter ("@ pagesize", sqldbtype.int));

Comm.Parameters [1] .value = Pagesize;

Comm.Parameters.Add (New Sqlparameter ("@ recordcount", sqldbtype.int);

Comm.Parameters [2] .direction = parameterDirection.output;

Comm.Parameters.Add (New Sqlparameter ("@ pagecount", sqldbtype.int));

Comm.Parameters [3] .direction = parameterdirection.output;

Comm.commandtype = commandtype.storedProcedure;

SqlDataAdapter DataAdapter = New SqlDataAdapter (Comm);

DataSet DS = New DataSet ();

DataAdapter.Fill (DS);

Recordcount = (int) comm.Parameters [2] .value;

PageCount = (int) comm.Parameters [3] .value;

Return DS;

}

// Bind data to DataGrid, simultaneously refresh data total record number private void DataGridDataBind ()

{

DataSet DS = GetCustomersdata (PageIndex, PageSize, Ref Recordcount, Ref PageCount);

THIS.DATAGRID1.VIRTUALITEMCOUNT = RecordCount;

THIS.DATAGRID1.DATASOURCE = DS;

THIS.DATAGRID1.DATABIND ();

}

Below is a variable variable attribute of paging

Public int pageCount

{

Get {return this.datagrid1.pagecount;

}

Public Int PageSize

{

Get {return this.datagrid1.pagesize;

}

Public int pageIndex

{

Get {return this.datagrid1.currentpageindex;}

Set {this.dataGrid1.currentpageindex = value;

}

Public int Recordcount

{

Get {return.

}

Register the DataGrid page event

// Patement Event Processing

Private void DataGrid1_pageIndexchanged (Object Source, System.Web.ui.WebControls.DataGridPageChangeDeventargs E)

{

DataGrid DG = (DataGrid) Source;

Dg.currentpageIndex = E.NewpageIndex;

DataGriddatabind ();

}

It is best to determine if the current page is loaded for the first time, preventing two data from being loaded repeatedly.

Private Void Page_Load (Object Sender, System.EventArgs E)

{

IF (! page.ispostback)

{

DataGriddatabind ();

}

}

The display interface is as follows:

There are no some parameters of the paging in this example, and we can further improve it.

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

New Post(0)
CopyRight © 2020 All Rights Reserved
Processed: 0.046, SQL: 9