The DB class and method in the code of this page can be queried in http://www.cnblogs.com/Deerjiadelu/p/7252769.html
(1) Asp.net--gridview Control--highlights the current row
protected voidGvquery_databound (Objectsender, GridViewRowEventArgs e) { //highlight the line where the cursor appears if(E.row.rowtype = = Datacontrolrowtype.datarow)//determines whether the current row is a data-bound row{E.row.attributes.add ("onmouseover","currentcolor=this.style.backgroundcolor;this.style.backgroundcolor= ' #6495ED '"); E.row.attributes.add ("onmouseout","This.style.backgroundcolor=currentcolor;"); } }
(2). Net--gridview Control--Highlight click Row
protected Override voidRender (System.Web.UI.HtmlTextWriter writer) {foreach(GridViewRow GVRinch This. Gvquery. Rows)//Highlight Selection Rows { if(GVR. RowType! = Datacontrolrowtype.datarow)return; if(GVR. Rowstate.hasflag (datacontrolrowstate.selected) = =false) {Gvr. Attributes.Add ("style","Background-color:none"); Gvr. attributes["onclick"] = Clientscript.getpostbackclienthyperlink ( This. Gvquery,"select$"+ GVR. RowIndex,true); }//ondblclick Double-click Else{GVR. Attributes.Add ("style","background-color: #F7CE90;"); } } Base. Render (writer); }
(3) Asp.net--gridview Control--Bind database data
protected void Page_Load (object sender, EventArgs e) { gvdatainit (); // GV Binding Data }
Public voidGvdatainit ()//binding GV Data { //Get query Criteria stringStrsid = This. Tbsmid. Text.trim (); stringStrsname = This. Tbsmname. Text.trim (); stringStrsex = This. Ddlsmsex. Selectedvalue.tostring (). Trim (); if(Strsex = ="Not Limited") {Strsex=""; } stringStrage = This. Tbsmage. Text.trim (); stringStrprovince = This. Ddlsmprovince. Selectedvalue.tostring (). Trim (); if(Strprovince = ="Not Limited") {strprovince=""; } stringStreducation = This. Ddlsmeducation. Selectedvalue.tostring (). Trim (); if(Streducation = ="Not Limited") {streducation=""; } stringStrgraduation = This. Tbsmgraduation. Text.trim (); DB DB=NewDB (); stringStrtitle ="SID as work number, sname as name, sex as gender, age as, province as province, education as, graduation as graduated, lmdate as maintenance date, lmtime as Dimension Protection time, lmuser as maintenance person";//fuzzy Conditional query-ignores this property when the condition is empty stringStrcontion ="(sid= '"+ Strsid +"' or '"+ Strsid +"' = ') and (Sname=n ')"+ Strsname +"' or '"+ Strsname +"' = ') and (Sex=n ')"+ Strsex +"' or '"+ Strsex +"' = ') and (age= ')"+ Strage +"' or '"+ Strage +"' = ') and (Province=n ')"+ Strprovince +"' or '"+ Strprovince +"' = ') and (Education=n ')"+ Streducation +"' or '"+ Streducation +"' = ') and (Graduation like N '%"+ Strgraduation +"% ' or '"+ Strgraduation +"' = ') and (active= ' Y ')"; stringstr ="Select"+ Strtitle +"From Staffinfo where"+ Strcontion +""; if(Db.sqlex (str) = =1) { This. Gvquery. DataSource =Db.redt (str); This. Gvquery. DataBind (); if( This. Gvquery. Rows.Count = =0) Response.Write ("<script>alert (' Look no man! Please re-enter the query criteria! '); location= ' staffmanage.aspx ' </script>"); } }
(4) Asp.net--gridview Control--pagination
Add three properties:
Allowpaging= "true"
Pagesize= "10"
Onpageindexchanging= "Gvquery_pageindexchanging"
protected void gvquery_pageindexchanging (object sender, Gridviewpageeventargs e) { = E . NewPageIndex; Gvdatainit (); }
(5) Asp.net--gridview Control--Export to Excel table
protected voidBtsmexcel_click (Objectsender, EventArgs e)//Export{gvquery. AllowPaging=false; Gvdatainit (); Response.Clear (); Response.AddHeader ("content-disposition","Attachment;filename=filename.xls"); Response.Charset="gb2312"; Response.ContentType="Application/vnd.xls"; System.IO.StringWriter STRWT=NewSystem.IO.StringWriter (); System.Web.UI.HtmlTextWriter HTW=NewHtmlTextWriter (STRWT); Gvquery. RenderControl (HTW); Response.Write (Strwt.tostring ()); Response.Flush (); Response.End (); Gvquery. AllowPaging=true; Gvdatainit (); } Public Override voidVerifyrenderinginserverform (Control Control)//Excel must overload this method { //Gridwiew must be placed inside the form tag with Runat=server//make sure that the control appears between the start and end tags of the HtmlForm control}
(6) Asp.net--gridview Control--first column insert ordinal
protected void gvquery_databound (object sender, GridViewRowEventArgs e) { if 0)// serial number { e.row.cells[01);} }
Asp.net--gridview Control--(1) Highlight the current row, (2) Highlight click Row, (3) Bind database data, (4) paging, (5) Export to Excel table, (6) First column insert ordinal