private void Page_Load (object sender, System.EventArgs e) {this.btnPreviousPage.Click = new System.EventHandler (this.NavigateToPage); this.btnNextPage.Click = new System.EventHandler (this.NavigateToPage); if (! IsPostBack) {StartIndex = 0; SqlConnection MyConnection = new SqlConnection ( "Data Source = NGB-INTRATST-1; User ID = Winc; Password = 123; initial catalog = Winc;"); SqlCommand MyCommand = new ( "Select num SqlCommand = COUNT (*) from Authors ", MyConnection); myConnection.Open (); SqlDataReader dr = myCommand.ExecuteReader (CommandBehavior.SingleRow); if (dr.Read ()) MyDataGrid.VirtualItemCount = (int) dr [" num "] Dr.close (); myconnection.close (); bindgridtosource (StartIndex, "Previous");}}
-------------------------------------------------- ----------------
private void BindGridToSource (int StartPosition, string GoToPage) {SqlConnection MyConnection = new SqlConnection ( "Data Source = NGB-INTRATST-1; User ID = Winc; Password = 123; initial catalog = Winc;"); SqlCommand MyCommand = null; switch (Gotopage) {case "Previous": mycommand = new SqlCommand ("SELECT TOP 5 AU_ID, AU_LNAME, AU_FNAME," "Phone, Address, City, State, Zip, Contract from Authors" " " Where Au_ID > @UID ORDER BY AU_ID ", MyConnection); if (StartPosition == 0) MyCommand.Parameters.Add (" @ uid ", sqldbtype.nvarchar, 4) .value =" 0 "; else mycommand.parameters.add ( "@uid", sqldbtype.nvarchar, 4) .Value = viewState [(MyDataGrid.currentpageIndex 1) .tostring ()]; Break; case "Next": mycommand = new sqlcommand ("SELECT TOP 5 au_id, au_lname , AU_FNAME, " " Phone, Address, City, State, ZIP, Contract from authors " " "Where au_id> @uid ORDER BY AU_ID", myconnection; mycommand.parameters.add ("@ uid", SqldbType .Nvarchar, 4) .value = myDataGrid.Items [4 ] .Cells [0] .TEXT;
Break;
MyConnection.open ();
SqlDataReader Dr = MyCommand.executeReader (); mydatagrid.datasource = DR; mydatagrid.database (); Dr.close (); myconnection.close ();
ViewState [(MyDataGrid.currentPageIndex 1) .tostring ()] = mydatagrid.Items [0] .Cells [0] .Text;}
-------------------------------------------------- ---------------------
public void NavigateToPage (object sender, System.EventArgs e) {string pageInfo = ((Button) sender) .CommandName; switch (pageInfo) {case "Previous": if (MyDataGrid.CurrentPageIndex> 0) MyDataGrid.CurrentPageIndex - = 1; else return; break; case "Next": if (MyDataGrid.CurrentPageIndex <(MyDataGrid.PageCount - 1)) MyDataGrid.CurrentPageIndex = 1; else return; break;} StartIndex = MyDataGrid.CurrentPageIndex * MyDataGrid.PageSize Bindgridtosource (StartIndex, PageInfo);
}