Large number of query paging show Microsoft Solutions _ Application Tips
Source: Internet
Author: User
Microsoft's Solution
Using System;
Using System.Data;
Using System.Data.SqlClient;
Using System.Drawing;
Using System.Windows.Forms;
public class Pagingsample:form
{
Form controls.
Button prevbtn = New button ();
Button nextbtn = New button ();
static DataGrid Mygrid = new DataGrid ();
Static label PAGELBL = new label ();
Paging variables.
static int pageSize = 10; Size of viewed page.
static int totalpages = 0; Total pages.
static int currentpage = 0; Current page.
static string Firstvisiblecustomer = ""; The determine location for move previous.
static string Lastvisiblecustomer = ""; Last customer in page to determine location for move next.
DataSet to bind to DataGrid.
Static DataTable custTable;
Initialize connection to database and DataAdapter.
Static SqlConnection nwindconn = new SqlConnection ("Data source=localhost;integrated security=sspi;initial Catalog= Northwind ");
Static SqlDataAdapter custDA = new SqlDataAdapter ("", nwindconn);
static SqlCommand selcmd = Custda.selectcommand;
public static void GetData (string direction)
{
Create SQL statement to return a page of records.
SelCmd.Parameters.Clear ();
Switch (direction)
{
Case "Next":
Selcmd.commandtext = "SELECT top" + pageSize + "CustomerID, CompanyName from Customers" +
"WHERE CustomerID > @CustomerId order by CustomerID";
SELCMD.PARAMETERS.ADD ("@CustomerId", SqlDbType.VarChar, 5). Value = Lastvisiblecustomer;
Break
Case "Previous":
Selcmd.commandtext = "SELECT top" + pageSize + "CustomerID, CompanyName from Customers" +
"WHERE CustomerID < @CustomerId order by CustomerID DESC";
SELCMD.PARAMETERS.ADD ("@CustomerId", SqlDbType.VarChar, 5). Value = Firstvisiblecustomer;
Break
Default
Selcmd.commandtext = "SELECT top" + pageSize + "CustomerID, CompanyName to Customers order by CustomerID";
Determine total pages.
SqlCommand totcmd = new SqlCommand ("Select Count (*) from Customers", nwindconn);
Nwindconn.open ();
int totalrecords = (int) totcmd.executescalar ();
Nwindconn.close ();
TotalPages = (int) math.ceiling (double) totalrecords/pagesize);
Break
}
Fill a temporary table with query results.
DataTable tmptable = new DataTable ("Customers");
int recordsaffected = Custda.fill (tmptable);
If table does not exist, create it.
if (custTable = null)
custTable = Tmptable.clone ();
The Refresh table if at least one record returned.
if (recordsaffected > 0)
{
Switch (direction)
{
Case "Next":
currentpage++;
Break
Case "Previous":
currentpage--;
Break
Default
CurrentPage = 1;
Break
}
Populate DataSet with the records and bind to grid.
GetData ("Default");
DataView CUSTDV = new DataView (custTable, "", "CustomerID", dataviewrowstate.currentrows);
Mygrid.setdatabinding (CUSTDV, "");
}
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.