DataGrid automatic summation, merge unit, sort

xiaoxiao2021-03-06  84

Previously, it was difficult to implement code reuse. ASP.NET solved this problem, the following is the DataGrid I wrote, inherit the DataGrid, add up to the sequence / full and automatic and auto-seeking function, the principle is simple, But a good code is reused.

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 Sorting Direction

///

Public String Sortdirection

{

get

{

If (ViewState ["SortDirection"] == NULL)

{

Return NULL;

}

Else

{

IF (ViewState ["SortDirection"]. TOSTRING () == "" "

{

Return NULL;

}

Else

{

Return ViewState ["sortdirection"]. TOSTRING ();

}

}

}

set

{

ViewState ["SortDirection"] = Value;

}

}

///

/// Protect Sortfield Sort by 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;

}

}

///

/// SQL query string

///

Public String SelectCommandText

{

get

{

IF (ViewState ["SELECTCOMMANDTEXT"] == NULL)

{

Return NULL;

}

Else

{

IF (ViewState ["SelectCommandText"]. TOSTRING () == "") {

Return NULL;

}

Else

{

Return ViewState ["SELECTCOMMANDTEXT"]. TOSTRING ();

}

}

}

set

{

ViewState ["SelectCommandText"] = Value;

}

}

///

/// Connection string

///

Public String SelectConnectionstring

{

get

{

If (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.itemdatabaseD = new system.web.ui.webcontrols.dataGriditeMeventHandler (this.DataGrid_itemdatabase;

}

Private void DataGrid_load (Object Sender, Eventargs E)

{

THIS.HORIZONTALALIGN = horizontalalign.center;

THIS.Allowsorting = true;

Arritem = new string [256];

DS = new dataset ();

}

///

/// Grid binding

///

/// query strings

/// Connection string

Public void bindgrid (String SelectCommandtext, String SelectConnectionstring)

{

THIS.SelectCommandtext = SELECTCOMMANDTEXT;

This.selectconnectionstring = selectconnectionstring ;. SELECTCONNECTIONSTRING

Bindgrid ();

}

///

/// Grid binding

///

/// query strings

/// connection object

Public void bindgrid (String SelectCommandtext, SqlConnection CN)

{

THIS.SelectCommandtext = SELECTCOMMANDTEXT;

THIS.SELECTCONNECTIONSTRING = cn.connectionstring;

Bindgrid ();

}

///

/// Grid binding, you must first set the selectcommmmandtext and selectconnectionstring properties

///

Public void bindgrid ()

{

IF (this.selectcommandtext! = null && this.selectconnectionstring! = NULL)

{

ADP = New SqldataAdapter (this.selectCommandtext, this.selectconnections;

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 == "dec")

{

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 == "dec")

{

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] .hizontalalign = horizontalalign.right;

}

Else

{

IF (txt == arritem [i] && txt! = "&& txt! = NULL)

{

E.Item.cells [i] .text = "";

}

Else

{

Arritem [i] = txt;

}

}

}

}

Catch (Exception ET)

{

}

}

}

}

Simple call:

Drag the component to the page, assume that the ID is DataGrid1:

Call: DataGrid1.bindgrid (String SelectConnectionString)

This saves the time to build the CONNTION DATAADAPTER DataSet and then bind.

You can also add the display time display format / digital display format, etc., and custom paging features, etc.

转载请注明原文地址:https://www.9cbs.com/read-106444.html

New Post(0)