A fully functional DataGrid page example

xiaoxiao2021-03-06  54

The .cs file is as follows:

Using system;

using System.Collections; using System.ComponentModel; using System.Data; using System.Drawing; using System.Web; using System.Web.SessionState; using System.Web.UI; using System.Web.UI.WebControls; using System .Web.ui.htmlcontrols; using system.data.sqlclient;

Namespace pu1_erp {///

/// DataGridPaging summary description. ///

Public Class DataGridPaging: System.Web.ui.page

{

Protected system.Web.ui.WebControls.label lblpagecount;

protected system.web.ui.webcontrols.label lblcurrentindex;

Protected system.web.ui.webcontrols.linkbutton btnfirst;

Protected system.Web.ui.WebControls.LinkButton Btnprev;

Protected system.Web.ui.WebControls.LinkButton btnnext;

Protected system.Web.ui.WebControls.LinkButton Btnlast;

Protected system.Web.ui.WebControls.DataGrid mydatagrid;

protected system.web.ui.webcontrols.label label1;

protected system.web.ui.webcontrols.label label2;

protected system.web.ui.webcontrols.label label3;

protected system.web.ui.webcontrols.textbox txt_grade_code;

protected system.web.ui.webcontrols.textbox txt_grade_name;

Protected system.Web.ui.WebControls.button btn_add;

protected system.web.ui.webcontrols.label lab_count;

Public system.data.sqlclient.sqlconnection cn = new sqlConnection ();

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

{

/ / Place the user code here to initialize the page

btnfirst.text = "The Home";

btnprev.text = "Previous page";

btnnext.text = "Next page";

btnlast.text = "Last page";

OpenDatabase ();

Bindgrid ();

}

private void BindGrid () {System.Data.SqlClient.SqlConnection con = cn; string str = "select * from SteelGrade order by steel type code"; System.Data.DataSet ds = new DataSet (); System.Data.SqlClient. SqlDataAdapter da = new SqlDataAdapter (str, con); da.Fill (ds, "JTQSpecStand"); MyDataGrid.DataSource = ds.Tables [ "JTQSpecStand"] DefaultView;. Lab_Count.Text = "current total steels" ds. Tables [ "JTQSpecStand"] Rows.Count.ToString ();. MyDataGrid.DataBind (); ShowStats ();} public void MyDataGrid_Page (object sender, DataGridPageChangedEventArgs e) {int startIndex; startIndex = MyDataGrid.CurrentPageIndex * MyDataGrid.PageSize; MyDataGrid.currentpageIndex = E.NewpageIndex; bindgrid (); showstats ();

}

Private void showstats ()

{LBLCurrentIndex.text = "" " (MyDataGrid.currentPageIndex 1) .tostring () " page "; lblpageCount.Text =" Total " mydataGrid.pagecount.toString () " Page ";

}

// Public void PagerButtonClick (Object sender, system.eventargs e) // {// string arg = (button) sender) .commandargument.toString (); string arg = e.commandargument.toString (); // switch ( Arg) // {// case "next": // if (MyDataGrid.currentpageIndex <(myDataGrid.pagecount - 1)) // {// mydatagrid.currentpageIndex = 1; ///} // Break; // / / Case "prev": // if (myDataGrid.currentpageindex> 0) // {// mydatagrid.currentpageindex - = 1; //} // Break; // Case "last": // myDataGrid.currentpageIndex = (MyDataGrid .Pagecount - 1); // Break; // // default: // mydatagrid.currentpageindex = system.convert.toint32 (arg); // // Break; ///} // // BindGrid (); // showstats (); // //} // Setting database connection string public void openatabase () {cn.connectionstring = "server = 172.16.2.2; database = movex12; uid = sa; pwd = tpcims"; CN .Open ();} # # Region Web Form Designer Generated Code Override Protected Void OnInit (Eventargs E) {// // Codegen: This call is required for the ASP.NET Web Form Designer. // InitializeComponent (); base.onit (e);} ///

/// Designer supports the required method - Do not use the code editor to modify the // / this method. ///

Private vidinitiRizeComponent ()

{

This.btnfirst.click = new system.eventhandler (this.btnfirst_click);

This.btnprev.click = new system.eventhandler (this.btnprev_click);

THIS.BTNNEXT.Click = new system.eventhandler (this.btnnext_click);

This.btnlast.click = new system.Eventhandler (this.btnlast_click);

THIS.BTN_ADD.CLICK = New System.EventHandler (this.btn_add_click);

This.Load = new system.eventhandler (this.page_load);} #ENDREGION

// Perform Next private void btnNext_Click (object sender, System.EventArgs e) {if (MyDataGrid.CurrentPageIndex <(MyDataGrid.PageCount - 1)) {MyDataGrid.CurrentPageIndex = 1; BindGrid (); ShowStats ();

}} // Perform the previous page private void btnprev_click (object sender, system.eventargs e) {if (MyDataGrid.currentpageIndex> 0) {MyDataGrid.currentpageIndex - = 1; bindgrid (); showstats ();

}

// Execute the top private void btnfirst_click (object sender, system.eventargs e) {myDataGrid.currentpageIndex = 0; bindgrid (); showstats ();

}

// Execute the last page private void btnlast_click (object sender, system.eventargs e) {myDataGrid.currentpageindex = (MyDataGrid.pagecount - 1); bindgrid (); showstats ();

}

// Add a new steel private void Btn_add_Click (object sender, System.EventArgs e) {try {Market_Cost.Steel ss = new Pu1_Erp.Market_Cost.Steel (); DataSet ds = ss.Grade (Txt_Grade_Name.Text.Trim (), TXT_GRADE_CODE.TEXT.TRIM ()); response.write ("