A DataGrid that automatically sums, merges, and sorts cells.
Author: snow qingsong
It was hard to implement in ASP before. Code Reuse. Asp.net solves this problem very well. The following is the DataGrid I wrote, inherited from the DataGrid, and added the ascending/descending order, full and merge cells, and automatic summation function. The principle is very simple, but the code can be 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>
/// Summary description for DataGrid.
/// </Summary>
[Defaultproperty ("text "),
Toolboxdata ("<{0}: DataGrid runat = Server> </{0}: DataGrid>")]
Public class DataGrid: 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;
}
}
/// <Summary>
/// Protect sortdirection
/// </Summary>
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;
}
}
/// <Summary>
/// Protect sortfield sorting Field
/// </Summary>
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;
}
}
/// <Summary>
/// SQL query string
/// </Summary>
Public String selectcommandtext
{
Get
{
If (viewstate ["selectcommandtext"] = NULL)
{
Return NULL;
}
Else
{
If (viewstate ["selectcommandtext"]. tostring () = "")
{
Return NULL;
}
Else
{
return viewstate ["selectcommandtext"]. tostring ();
}< BR >}< br> set
{< br> viewstate ["selectcommandtext"] = value;
}< BR >}< br> ///
// connection string
///
Public String selectconnectionstring
{< br> Get
{< br> If (viewstate ["selectconnectionstring"] = NULL)
{< br> return NULL;
}< br> else
{< br> return viewstate ["selectconnectionstring"]. tostring ();
}< BR >}< br> set
{< br> viewstate ["selectconnectionstring"] = value;
}< BR >}< br> Public datatable bindtable;
Public DataGrid ()
{< br> This. init + = new system. eventhandler (this. datagrid_init);
}< br> 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 ();
}
/// <Summary>
/// Bind a grid
/// </Summary>
/// <Param name = "selectcommandtext"> query string </param>
/// <Param name = "selectconnectionstring"> connection string </param>
Public void bindgrid (string selectcommandtext, string selectconnectionstring)
{
This. selectcommandtext = selectcommandtext;
This. selectconnectionstring = selectconnectionstring;
Bindgrid ();
}
/// <Summary>
/// Bind a grid
/// </Summary>
/// <Param name = "selectcommandtext"> query string </param>
/// <Param name = "cn"> connection object </param>
Public void bindgrid (string selectcommandtext, sqlconnection CN)
{
This. selectcommandtext = selectcommandtext;
This. selectconnectionstring = cn. connectionstring;
Bindgrid ();
}
/// <Summary>
/// Bind the grid. The selectcommmandtext and selectconnectionstring attributes must be set first.
/// </Summary>
Public void bindgrid ()
{
If (this. selectcommandtext! = NULL & this. selectconnectionstring! = NULL)
{
ADP = new sqldataadapter (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 <Ds. Tables ["Temp"]. Columns. Count; I ++)
{
If (Ds. Tables ["Temp"]. Columns [I]. columnname = This. sortfield)
{
Sortfieldindex = I;
Break;
}
}
String sortdirectionimg = "▲ ";
If (this. sortdirection = "DESC ")
{
Sortdirectionimg = "Hangzhou ";
}
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 <bindtable. Columns. Count; 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 ("delimiter ","");
Bindgrid ();
}
Private void datagrid_itemdatabound (Object sender, system. Web. UI. webcontrols. datagriditemeventargs E)
{
Try
{
String TXT = "";
For (INT I = 0; I <E. Item. cells. Count; 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)
{
}
}
}
}
simple call:
drag the component to the page. Assume that the ID is datagrid1:
call: datagrid1.bindgrid (string selectcommandtext, string selectconnectionstring)
This saves the time for conntion dataadapter dataset re-binding.
You can also add the display time display format and number display format to the itemdatabound event, and customize the paging function.