DataGrid pagination usage Summary

Source: Internet
Author: User
Default paging mode:Select "allow pagination"; page size; page navigation settings, which can be the upper/lower mode, or the start mode of the "page navigation" button in the page number format; private void datashow () // bind data
{
String SQL = "server = 127.0.0.1; database = ltp; user id = sa; password = ";
SqlConnection mycon = new SqlConnection (SQL); string selsql = "select * from data ";
SqlDataAdapter da = new SqlDataAdapter (selsql, mycon); DataSet ds = new DataSet ();
Da. Fill (ds, "data"); this. DataGrid1.DataSource = ds. Tables ["data"];
This. DataGrid1.DataBind ();

} RESPONSE event PageIndexChanged () this. Maid = e. NewPageIndex;
Datashow (); Default paging mode for custom navigation controlsCurrent page: this. label1.Text = (this. dataGrid1.CurrentPageIndex + 1 ). toString (); Because CurrentPageIndex starts from 0, the total number of pages must be + 1: this. label2.Text = this. dataGrid1.PageCount. toString (); // the first page of this. dataGrid1.CurrentPageIndex = 0; // previous page if (this. dataGrid1.CurrentPageIndex> 0)
{
This. Maid-= 1;
This. datashow ();
} // Next page
If (this. Maid-1 ))
{
This. Maid + = 1;
This. datashow ();
} // The last page this. Maid-1 and then datashow ();     Custom Data paging-very important! (Improves performance efficiency)Each time this. datashow (); is used to extract all the data, which reduces the efficiency. The correct method is as follows: 1. Select "allow pagination", "allow custom pagination", and page size. 2. Add the navigation button to set the CommandName attribute, previous, next3, code: // record the start index of each page
Int startindex; private void Page_Load (object sender, System. EventArgs e)
{
// Custom button event
This. btnprevious. Click + = new System. EventHandler (this. NavigateToPage );
This. btnnext. Click + = new System. EventHandler (this. NavigateToPage); // or OnCommand = "NavigateToPage" if (! IsPostBack)
{
Startindex = 0; // obtain the number of records of the data source and assign them to the DataGrid1 string constr = "server = 127.0.0.1; database = ltp; user id = sa; password = ";
SqlConnection mycon = new SqlConnection (constr );
Mycon. Open (); string SQL = "Total select count (*) from data ";
SqlCommand com = new SqlCommand (SQL, mycon); SqlDataReader dr = com. ExecuteReader (CommandBehavior. SingleRow );
If (dr. Read ())
This. DataGrid1.VirtualItemCount = (int) dr ["Total"];
Dr. Close ();
Mycon. Close ();

//
This. bindGrid (startindex, "previous ");}
} // Custom button event
Private void NavigateToPage (object sender, System. EventArgs e)
{
String pageinfo = (Button) sender). CommandName;
Switch (pageinfo)
{
Case "previous ":
If (this. DataGrid1.CurrentPageIndex> 0)
{
This. Maid-= 1;

}
Break; case "next ":
If (this. Maid-1 ))
{
This. Maid + = 1;

}
Break;} // get the start Index
Startindex = this. Maid * this. Maid size;
// Rebind
This. bindGrid (startindex, pageinfo );} // Extract the required data records from the data source-method 2 (tables with int numbers)
Private void bindGrid2 (int startindex, string pageinfo)
{
String constr = "server = 127.0.0.1; database = ltp; user id = sa; password = ";
SqlConnection mycon = new SqlConnection (constr );
Mycon. Open (); string SQL = "select top 5 * from data where Sn> =" + startindex + "order by Sn ";
SqlDataAdapter da = new SqlDataAdapter (SQL, mycon); DataSet ds = new DataSet ();
Da. Fill (ds, "data ");
This. DataGrid1.DataSource = ds. Tables ["data"];
This. DataGrid1.DataBind ();

Mycon. Close ();}   // Extract the required data records from the data source-method 1 (sorted by a string column)
Private void bindGrid (int startindex, string pageinfo)
{
String constr = "server = 127.0.0.1; database = ltp; user id = sa; password = ";
SqlConnection mycon = new SqlConnection (constr );
Mycon. Open (); SqlCommand com = new SqlCommand (); switch (pageinfo)
{
Case "previous ":
String SQL = "select top 5 * from data where shareholding name >=@ id order by shareholding name ";
Com = new SqlCommand (SQL, mycon); // com = new SqlCommand ("select top 5 * from data where shareholding name >=@ id order by shareholding name", mycon );

If (startindex = 0)
{
Com. Parameters. Add ("@ id", SqlDbType. NVarChar, 10). Value = "";
}
Else
{
// Start
Com. Parameters. Add ("@ id", SqlDbType. NVarChar, 10). Value = ViewState [(this. DataGrid1.CurrentPageIndex + 1). ToString ()];
// Com. Parameters. Add ("@ id", SqlDbType. NVarChar, 10). Value = this. DataGrid1.Items [0]. Cells [1]. Text;
}
Break; case "next ":
String sql2 = "select top 5 * from data where shareholding Name> @ id order by shareholding name ";
Com = new SqlCommand (sql2, mycon );

// Assign the column value of the last row to the start of the next page.
Com. Parameters. Add ("@ id", SqlDbType. NVarChar, 10). Value = this. DataGrid1.Items [4]. Cells [1]. Text;
Break;
} SqlDataReader dr = com. ExecuteReader ();
This. Maid = dr;
This. DataGrid1.DataBind ();
Dr. Close ();
Mycon. Close (); // obtain the column value of the first row of the current start.
ViewState [(this. Maid + 1). ToString ()] = this. Maid [0]. Cells [1]. Text;
}

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.