Automatic DataGrid summation, merge cells, and sorting

Source: Internet
Author: User
Automatic DataGrid summation, merge cells, and sorting

It was hard to implement in ASP before.CodeReuse. 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 description for DataGrid.
///
[Defaultproperty ("text "),
Toolboxdata ("<{0}: DataGrid runat =" server ">")]
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;
}
}
///
/// Protect sortdirection
///

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 sorting Field
///
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. 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 a grid
///
/// Query string
/// Connection string
Public void bindgrid (string selectcommandtext, string selectconnectionstring)
{
This. selectcommandtext = selectcommandtext;
This. selectconnectionstring = selectconnectionstring;
Bindgrid ();

}
///
/// Bind a grid
///
/// Query string
/// Connection object
Public void bindgrid (string selectcommandtext, sqlconnection CN)
{
This. selectcommandtext = selectcommandtext;
This. selectconnectionstring = cn. connectionstring;
Bindgrid ();
}
///
/// Bind the grid. The selectcommmandtext and selectconnectionstring attributes must be set first.
///
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 {
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 {
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 [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 and assume that the ID is datagrid1:
Call: datagrid1.bindgrid (string selectcommandtext, string selectconnectionstring)
This saves the time for conntion dataadapter dataset rebinding.
You can also add the display time display format and number display format to the itemdatabound event, as well as the custom paging function.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.