Default paging mode:
Select "Allow Pack"; page size; page navigation settings, can be up and down, you can also use the page number mode
The "page navigation" button can be set;
Private void DataShow () // Binding 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.DataGrid1.currentpageIndex = E.NewpageIndex;
DataShow ();
Customize the default paging mode of the navigation control
Current page: this.label1.text = (this.DataGrid1.currentpageIndex 1) .tostring ();
Because CurrentPageIndex starts from 0, it is necessary 1
Total number: this.label2.text = this.datagrid1.pagecount.tostring ();
//First page
this.dataGrid1.currentpageIndex = 0;
// Previous page
IF (this.DataGrid1.currentpageIndex> 0)
{
THIS.DATAGRID1.CURRENTPAGEINDEX- = 1;
THIS.DATASHOW ();
}
// Next page
IF (this.DataGrid1.currentpageIndex <(this.DataGrid1.pagecount-1))
{
THIS.DATAGRID1.CURRENTPAGEINDEX = 1;
THIS.DATASHOW ();
}
//the last page
this.DataGrid1.currentpageIndex = this.dataGrid1.pagecount-1
Finally datashow ();
Custom data paging - very important! (Improve performance efficiency)
Each time this.DataShow (); is to extract all the data, but reducing efficiency.
The correct way:
1. Select "Allow Patement"; "Allow custom paging"; page size.
2, add the navigation button, set the commandName property, Previous, Next
3, code:
// Record the beginning of each page index 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;
/ / Get the number of records of the data source, and assigned to DataGrid1
String constr = "server = 127.0.0.1; database = LTP; user ID = sa; password =";
SqlConnection mycon = new sqlConnection (constr);
Mycon.open ();
String SQL = "SELECT total = count (*) from data";
SQLCommand COM = New SQLCOMMAND (SQL, MyCon);
SqlDataReader Dr = com.executeReader (Commandbehavior.singlerow);
IF (Dr.Read ())
THIS.DATAGRID1.VIRTUALITEMCOUNT = (int) DR ["total number";
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.DATAGRID1.CURRENTPAGEINDEX- = 1;
}
Break;
Case "Next":
IF (this.DataGrid1.currentpageIndex <(this.DataGrid1.pagecount-1))
{
THIS.DATAGRID1.CURRENTPAGEINDEX = 1;
}
Break;
}
// Get the beginning of the index StartIndex = this.dataGrid1.currentpageIndex * this.dataGrid1.pageSize;
/ / Re-bound this.bindgrid (StartIndex, PageInfo);
}
// Extract the required data record from the data source - Method 2 (Table with INT serial number) 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 Serial> =" StartIndex "Order By Serial";
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 record from the data source - Method 1 (in string of string) 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 Hold Name> = @ ID ORDER BY Hold Name";
COM = New SQLCOMMAND (SQL, MyCon);
// com = new sqlcommand ("SELECT TOP 5 * from Data Where Hold Name> = @ ID ORDER BY Hold 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 Hold Name> @ID ORDER BY Hold Name";
COM = New SQLCOMMAND (SQL2, MyCon);
// Assassin to the next page to start com.parameters.add ("@ id", sqldbtype.nvarchar, 10) .Value = this.DataGrid1.items [4] .cells [1] .text;
Break;
}
SqlDataReader Dr = com.executeRead ();
THIS.DATAGRID1.DATASOURCE = DR;
THIS.DATAGRID1.DATABIND ();
Dr.close ();
Mycon.close ();
// Re-get the current column value viewState [(this.DataGrid1.currentpageIndex 1) .tostring ()] = this.dataGrid1.items [0] .Cells [1] .TEXT;
}