Using system;
Using system.Web.ui;
Using system.Web.ui.webcontrols;
Using system.componentmodel;
Using system.data;
Using system.data.sqlclient;
Namespace SunService
{
///
/// Summary Description for DataGrid.
///
[DefaultProperty ("Text"),
ToolboxData ("<{0}: DataGrid Runat =" Server ">")]
Public class data, system.web.ui.webcontrols.DataGrid
{
PRIVATE STRING TEXT;
Private SqldataAdapter ADP;
Private dataset ds;
PRIVATE DATAVIEW View;
Private string [] arritem;
[Bindable (TRUE),
Category ("APPEARANCE"),
DEFAULTVALUE ("")]
Public String Text
{
get
{
Return TEXT;
}
Set {text = value;}} /// /// Protect SortDirection Sort ////
Public string sortdirection {get {i (viewState ["sortdirection"] == null) {return null;} else {i (ViewState ["sortdirection"]. TOSTRING () == "") {Return null;} else {Return ViewState ["SortDirection"]. TOSTRING ();}}} set {viewState ["sortdirection"] = value;}} /// /// Protect Sortfield Sort /// PUBLIC STRING SORTFIELD {Get {IF (ViewState "Sortfield"] == null) {return null;} else {if (ViewState ["sortfield"]. TOSTRING () == "") {Return null;} else {return viewState ["sortfield"]. Tostring () }}} set {ViewState ["Sortfield"] = value;}} ///// PUBLIC STRING SELECTCOMMANDTEXT {Get {if (ViewState ["SelectCommandText"] == null) {Return Else {IF (ViewState ["SelectCommandText"]. TOSTRING () == "" "{Return null;} else {
Return ViewState ["SELECTCOMMANDTEXT"]. TOSTRING ();}}} set {viewState ["selectcommandtext"] = value;}} /// // connection string // public string selectconnectionstring {get {ix (viewstate " "selectConnectionString"] == null) {return null;} else {. return ViewState [ "selectConnectionString"] ToString ();}} set {ViewState [ "selectConnectionString"] = value;}} public DataTable Bindtable; public DataGrid () {this.Init = new System.EventHandler (this.DataGrid_Init);} private void DataGrid_Init (object sender, EventArgs e) {this.Load = new System.EventHandler (this.DataGrid_Load); this.SortCommand = new System.Web. UI.WebControls.DataGridSortCommandEventHandler (this.DataGrid_SortCommand); this.ItemDataBound = new System.Web.UI.WebControls.DataGridItemEventHandler (this.DataGrid_ItemDataBound);
} Private void DataGrid_load (Object Sender, Eventargs E) {this.horizontalalign = horizontalalign.center; this.allowsorting = true; arritem = new string [256]; ds = new dataset ();
}
/// /// /// /// bind the GRID connection string query string /// public void BindGrid (string selectCommandText, string selectConnectionString) {this.selectCommandText = selectCommandText; this.selectConnectionString = selectConnectionString; BindGrid () ;
Binding ///} /// /// grid connection object /// /// Query String public void BindGrid (string selectCommandText, SqlConnection cn) {this.selectCommandText = selectCommandText; this.selectConnectionString = cn.ConnectionString; BindGrid ();} /// /// grid bound, and must set selectCommmandText SelectConnectionString properties /// public void BindGrid () {if {adp = new SqlDataAdapter ((this.selectCommandText = null && this.selectConnectionString = null!!) THIS.SELECTCOMMANDTEXT, this.SelectConnectionsTRING; adp.fill (DS, "TEMP"); view = ds.tables ["temp"]. defaultview; if (this.sortfield! = null) {view.sort = this.sortfield "" this.SortDirection; int sortfieldindex = 0; for (int i = 0; i {if (ds.tables ["temp"]. Columns [i] .columnname == this.sortfield) {sortfieldIndex = i; BREAK }} String SortdirectionImg = "▲"; if (this.Sortdirection == "DESC") {sortdirectionimg = "▼";
} if (this.Sortfield! = this.datakeyfield) {ds.tables ["temp"]. Columns [sortfieldindex] .columnname = sortdirectionimg;}
} Bindtable = ds.tables ["TEMP"]; DATAROW ROW = BindTable.NewRow (); Row [0] = "Total:"; for (int i = 1; i {type t = bindtable.columns [i]. DataType; if (t == typeof (decimal) || T == TypeOf (Double) || T == Typeof (INT16) || T == TypeOf (int32) || T == Typeof (int64) || T == TYPEOF (uint16) || t == typeof (uint32) || t == typeof (int64)) {row [i] = 0; Foreach (DataRow R in bindtable.rows) {Try {row [i] = Double.Parse (row [i] .tostring ()) Double.Parse (R [i] .tostring ());} catch (Exception et) {
}
}}} Bindtable.rows.add (row);
THIS.DataSource = view; this.databind ();
} else {
}} Private void DataGrid_SortCommand (object source, System.Web.UI.WebControls.DataGridSortCommandEventArgs e) {if (this.SortDirection == "DESC") {this.SortDirection = "ASC";} else {this.SortDirection = "DESC "}
This.Sortfield = E.Sortexpression; this.Sortfield = this.sortfield.replace ("▲", ""); this.sortfield = this.Sortfield.Replace ("▼", "");
Bindgrid ();
Private void DataGrid_itemdatabase (Object sender, system.web.ui.webcontrols.dataGriditeMeventArgs e) {try {string txt = ""; for (int i = 0; i {// E.Item.cells [i] .wrap = false ; txt = E.Item.cells [i] .text.trim ();
IF (myclass.isdouble (txt)) {E.Item.cells [i] .horizontalalign = horizontalalign.right;} else {if (txt == arritem [i] && txt! = "&& txt! = null) {e. Item.cells [i] .text = "";} else {arritem [i] = txt;}}}} catch (Exception et) {
}
}}}
Call Simplified: Drag the component to the page, assume the ID to be DataGrid1: call: DataGrid1.bindGrid (String SelectConnectionString) This time the CONNTION DATAADAPTER DATASET is blended. You can also display the display time display format / number In the ItemDatabase, the display format is displayed, there is custom paging function, etc.