Implement DataGrid similar to Windows Resource Manager

Source: Internet
Author: User

In the DataGrid, we can achieve the effect similar to that of Windows resource manager, that is, sort columns. The color of this column is different from that of other columns. The following is the implementation code:

C # code:

Datagridlikewindowsexplorer. aspx

<Meta name = "generator" content = "Microsoft Visual Studio 7.0"> <meta name = "code_language" content = "C #"> <meta name = "vs_defaultclientscript" content =" javaScript "> <meta name =" vs_targetschema "content =" http://schemas.microsoft.com/intellisense/ie5 "> <br/> <style type =" text/CSS ">. gridtooltip {border-Right: Black 1px solid; padding-Right: 4px; border-top: Black 1px solid; padding-left: 4px; Z-INDEX: 200; left: 0px; visibility: hidden; padding-bottom: 4px; Font: 9pt bold; border-left: Black 1px solid; width: 150px; color: White; padding-top: 4px; border-bottom: black 1px solid; position: absolute; top: 0px; Background-color: # ff3316 }</style> <p> <placeholder id = "tips_placeholder" runat = "server"> </placeholder>

Datagridlikewindowsexplorer. aspx. CS

Using system; using system. collections; using system. componentmodel; using system. data; using system. data. sqlclient; using system. drawing; using system. web; using system. web. caching; using system. web. sessionstate; using system. web. ui; using system. web. UI. webcontrols; using system. web. UI. htmlcontrols; namespace aspxwebcs {/// <br/> <summary> /// abstract description of the datagridlikewindowsexplorer. /// </Summary> <p> public class datagridlikewindowsexplorer: system. web. UI. page {protected system. web. UI. webcontrols. dataGrid winexplorerview_datagrid; protected system. data. sqlclient. sqldataadapter m_sqldataadapter; protected system. data. sqlclient. sqlcommand m_sqlselectcommand; protected system. data. sqlclient. sqlconnection m_sqlconnection; protected system. data. dataset m_dsemployees; protected s Ystem. data. dataview m_dvemployees; protected system. collections. hashtable colheadermap; protected string m_strsortexpr; protected string m_strsortorder; protected int m_isortcolumnidx; protected system. web. UI. webcontrols. placeholder tips_placeholder; protected string strconn = "Data Source = .; user ID = sa; Password =; initial catalog = northwind; "; private void page_load (Object sender, system. eventargs E) {M_isortcolumnidx =-1; m_strsortexpr = ""; m_strsortorder = ""; processviewstate (); preparecolumnheadermap (); If (! Ispostback) {bindgridtoview () ;}} private void preparecolumnheadermap () {colheadermap = new hashtable (); int idx = 0; foreach (datagridcolumn Col in seconds) {colheadermap [col. sortexpression] = idx ++;} private void retrievedata () {If (null = cache ["employeesds"]) {string TMP = "select lastname, firstname, title, birthdate, city from employees "; m_sqlconnection = N EW sqlconnection (strconn); m_sqlselectcommand = new sqlcommand (TMP, m_sqlconnection); m_sqldataadapter = new sqldataadapter (m_sqlselectcommand); m_dsemployees = new dataset ("employees"); updated (updated ); cache. insert ("employeesds", m_dsemployees, null, datetime. now. addminutes (1), cache. noslidingexpiration);} else {m_dsemployees = (Dataset) cache ["employeesds"];} private Vo Id processviewstate () {If (null! = Viewstate ["sortexpr"]) {m_strsortexpr = viewstate ["sortexpr"]. tostring () ;}if (null! = Viewstate ["sortorder"]) {m_strsortorder = viewstate ["sortorder"]. tostring () ;}} private void bindgridtoview () {string strsort = ""; if (0! = M_strsortexpr.length) {strsort = m_strsortexpr; If (0! = M_strsortorder.length) {strsort + = ("" + m_strsortorder) ;}} retrievedata (); m_dvemployees = new dataview (Bytes [0], "", strsort, dataviewrowstate. currentrows); winexplorerview_datagrid.datasource = m_dvemployees; winexplorerview_datagrid.databind ();} private color getsortcolumncolor () {If (null = This. m_strsortorder | string. empty = This. m_strsortorder | 0 = This. m_strsort Order. length) {return color. gold;} If (m_strsortorder.compareto ("ASC") = 0) {return color. gold;} else {return color. blanchedalmond ;}} private void onpageindexchange (Object source, system. web. UI. webcontrols. datagridpagechangedeventargs e) {winexplorerview_datagrid.currentpageindex = E. newpageindex; bindgridtoview ();} private void onsortview (Object source, system. web. UI. webcontrols. datagri Dsortcommandeventargs e) {m_strsortexpr = E. sortexpression; viewstate ["sortexpr"] = m_strsortexpr; If (0 = m_strsortorder.length) {m_strsortorder = "ASC";} else if (m_strsortorder.compareto ("ASC") = 0) {m_strsortorder = "DESC";} else {m_strsortorder = "ASC";} viewstate ["sortorder"] = m_strsortorder; // locate the column number of the Click Event m_isortcolumnidx = convert. toint32 (this. colheadermap [m_strsortexpr]); B Indgridtoview ();} private void onitemcreated (Object sender, system. web. UI. webcontrols. datagriditemeventargs e) {If (E. item. itemtype = listitemtype. header) {int idx = 0; foreach (tablecell CL in E. item. cells) {Cl. attributes. add ("onmouseover", "showheadertip (" + idx. tostring () + ");"); CL. attributes. add ("onmouseout", "hideheadertip (" + idx. tostring () + ");"); idx ++ ;}} if (E. item. itemtype = L Istitemtype. Item | E. Item. itemtype = listitemtype. alternatingitem) {If (-1! = M_isortcolumnidx) {e. item. cells [m_isortcolumnidx]. backcolor = getsortcolumncolor () ;}} protected override void onprerender (eventargs e) {// tooltip text int ncount = This. winexplorerview_datagrid.columns.count; For (INT I = 0; I <strong> author blog: </strong> http://blog.csdn.net/littlehb/

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.