<asp:gridview id= "GridView1" runat= "Server" allowsorting= "True" cellpadding= "3" Font-size= "9pt" onsorting= "gridview1_sorting" backcolor= "white" bordercolor= "#CCCCCC" borderstyle= "None" Borderwidth= "1px" > <footerstyle backcolor= "White" forecolor= "#000066"/> <Columns> <asp:boundfield datafield= "ID number" headertext= "User ID" sortexpression= "ID number"/> <asp:boundfield datafield= "name" headertext= "User name" sortexpression= "name"/> <asp:boundfield datafield= "Employee Sex" headertext= "gender" sortexpression= "Employee Sex"/> <asp: BoundField datafield= "Home Address" headertext= "Home Address" sortexpression= "Home Address"/> </Columns> <rowstyle forecolor= "# 000066 "/> <selectedrowstyle backcolor=" #669999 "font-bold=" True "forecolor=" white "/> <pagerstyle Backcolor= "White" forecolor= "#000066" horizontalalign= "left"/> Effect Chart:
Background code:
Using System;
Using System.Data;
Using System.Configuration;
Using System.Collections;
Using System.Web;
Using System.Web.Security;
Using System.Web.UI;
Using System.Web.UI.WebControls;
Using System.Web.UI.WebControls.WebParts;
Using System.Web.UI.HtmlControls;
Using System.Data.SqlClient; Public partial class Default4:System.Web.UI.Page {SqlConnection sqlcon. String Strcon = "Data source= (local);D atabase= North Wind trade; Uid=sa;
Pwd=sa "; protected void Page_Load (object sender, EventArgs e) {DropDownList DDL; if (!
IsPostBack) {String sqlstr = "SELECT Top 5 * from flying Fox Studio"; Sqlcon = new SqlConnection (Strcon);
SqlDataAdapter Myda = new SqlDataAdapter (Sqlstr, Sqlcon);
DataSet myds = new DataSet (); Sqlcon.
Open (); Myda.
Fill (myDS, "Flying Fox Studio");
Gridview1.datasource = myds;
Gridview1.databind (); for (int i = 0; I <= gridview1.rows.count-1 i++) {DataRowView mydrv = myds. Tables["Flying Fox Studio"].
Defaultview[i]; if (convert.tostring (mydrv["employee Sex"]). Trim () = = "True" {DDL = (DropDownList) gridview1.rows[i]. FindcOntrol ("DropDownList1"); Ddl.
SelectedIndex = 0; if (convert.tostring (mydrv["Employee Sex")). Trim () = = "False" {DDL = (DropDownList) gridview1.rows[i].
FindControl ("DropDownList1"); Ddl.
SelectedIndex = 1; }} sqlcon.
Close (); } public SqlDataReader Ddlbind () {String sqlstr = "SELECT DISTINCT employee sex from flying Fox Studio"; Sqlcon = new SqlConnection (Strcon)
;
SqlCommand sqlcom = new SqlCommand (sqlstr, Sqlcon); Sqlcon.
Open (); Return sqlcom.
ExecuteReader ();
}
foreground main code:
<asp:gridview id= "GridView1" runat= "Server" allowsorting= "True" cellpadding= "3" font-size= "9pt" backcolor= "white" Bordercolor= "#CCCCCC" borderstyle= "None" borderwidth= "1px" > <footerstyle backcolor= "White" forecolor= "#000066 "/> <Columns> <asp:boundfield datafield=" ID number headertext= "User ID" sortexpression= "ID number"/> <asp:
BoundField datafield= "name" headertext= "User name" sortexpression= "name"/> <asp:templatefield "employee Sex" > <ItemTemplate> <asp:dropdownlist id= "DropDownList1" runat= "server" Datasource= ' <%# ddlbind ()%> ' datavaluefield= "Employee Sex" datatextfield= "employee Sex" > </asp:DropDownList> </ItemTemplate> </asp:
templatefield> <asp:boundfield datafield= "Home Address" headertext= "Home Address" sortexpression= "Home Address"/> </Columns> <rowstyle forecolor= "#000066"/> <selectedrowstyle backcolor= "#669999" font-bold= "True" forecolor= "white"/ > <pagerstyle backcolor= "White" forecolor= "#000066" horizontalalign= "left"/> &lT HeaderStyle backcolor= "#006699" font-bold= "True" forecolor= "white"/> </asp:GridView>
5.GridView combined with checkbox:
Effect Chart:
Background code:
Using System;
Using System.Data;
Using System.Configuration;
Using System.Web;
Using System.Web.Security;
Using System.Web.UI;
Using System.Web.UI.WebControls;
Using System.Web.UI.WebControls.WebParts;
Using System.Web.UI.HtmlControls;
Using System.Data.SqlClient; Public partial class Default5:System.Web.UI.Page {SqlConnection sqlcon. String Strcon = "Data source= (local);D atabase= North Wind trade; Uid=sa;
Pwd=sa "; protected void Page_Load (object sender, EventArgs e) {if (!
IsPostBack) {bind ();}} protected void Checkbox2_checkedchanged (object sender, EventArgs e) {for (int i = 0; I <= gridview1.rows.count-1; i+ +) {checkbox Cbox = (checkbox) Gridview1.rows[i].
FindControl ("CheckBox1"); if (checkbox2.checked = = True) {Cbox.
Checked = true; else {Cbox.
Checked = false;
}} protected void Button2_Click (object sender, EventArgs e) {sqlcon = new SqlConnection (Strcon);
SqlCommand sqlcom; for (int i = 0; I <= gridview1.rows.count-1 i++) {checkbox Cbox = (CheckBox) gridview1.rows[i]. FiNdcontrol ("CheckBox1"); if (Cbox. Checked = = True) {string sqlstr = "Delete from flying Fox Studio where ID number = ' + gridview1.datakeys[i]."
Value + "'";
sqlcom = new SqlCommand (sqlstr, Sqlcon); Sqlcon.
Open (); Sqlcom.
ExecuteNonQuery (); Sqlcon.
Close ();
} bind (); } protected void button1_click (object sender, EventArgs e) {checkbox2.checked = false; for (int i = 0; I <= GridView1. rows.count-1; i++) {checkbox cbox = (checkbox) Gridview1.rows[i].
FindControl ("CheckBox1"); Cbox.
Checked = false;
} public void Bind () {String sqlstr = ' SELECT Top 5 * Fly Fox Studio '; Sqlcon = new SqlConnection (Strcon);
SqlDataAdapter Myda = new SqlDataAdapter (Sqlstr, Sqlcon);
DataSet myds = new DataSet (); Sqlcon.
Open (); Myda.
Fill (myDS, "Tb_member");
Gridview1.datasource = myds;
Gridview1.datakeynames = new string[] {"Identity card number"};
Gridview1.databind (); Sqlcon.
Close ();
}
}
foreground main code:
<asp:gridview id= "GridView1" runat= "Server" allowsorting= "True" cellpadding= "3" font-size= "9pt" backcolor= "white" Bordercolor= "#CCCCCC" borderstyle= "None" borderwidth= "1px" > <footerstyle backcolor= "White" forecolor= "#000066 "/> <Columns> <asp:TemplateField> <ItemTemplate> <asp:checkbox id=" CheckBox1 "runat=" Server " /> </ItemTemplate> </asp:TemplateField> <asp:boundfield datafield= "ID number" headertext= "User ID" sortexpression= "ID number"/> <asp:boundfield datafield= "name" headertext= "User name" sortexpression= "name"/> <asp: BoundField datafield= "Home Address" headertext= "Home Address" sortexpression= "Home Address"/> </Columns> <rowstyle forecolor= "# 000066 "/> <selectedrowstyle backcolor=" #669999 "font-bold=" True "forecolor=" white "/> <pagerstyle Backcolor= "White" forecolor= "#000066" horizontalalign= "left"/> <headerstyle backcolor= "#006699" font-bold= " True "forecolor=" white "/> </asp:GridView> <asp:checkbox id=" CheckBox2"runat=" Server "font-size=" 9pt "oncheckedchanged=" checkbox2_checkedchanged "text=" Select All "/> <asp:button ID=" Button1 "runat=" Server "font-size=" 9pt "text=" Cancel "onclick=" Button1_Click "/> <asp:button id=" Button2 "runat="
Server "font-size=" 9pt "text=" "Delete" onclick= "button2_click"/>
6. Change the background color of the row when you move the mouse to a certain row of the GridView method one:
Effect Chart:
Practice:
Double-click the Onrowdatabound event of the GridView;
In the background The GridView1_RowDataBound () method adds code, and the final code looks like this:
protected void GridView1_RowDataBound (object sender, GridViewRowEventArgs e)
{
int i;
Execute loops to ensure that each piece of data can be updated for
(i = 0; i < GridView1.Rows.Count; i++)
{
//first determine if it is a data row
if (E.row.rowtype = = Datacontrolrowtype.datarow)
{
//change the background color e.row.attributes.add when the mouse hovers
("onmouseover", "c= This.style.backgroundcolor;this.style.backgroundcolor= ' #00A9FF ');
Restores the background color
e.row.attributes.add ("onmouseout", "This.style.backgroundcolor=c") when the mouse is moved
;
Foreground code:
<html xmlns= "http://www.w3.org/1999/xhtml" > <head runat= "Server" > <title> implementing the row background color of the GridView
Qingqing Moon http://blog.csdn.net/21aspnet </title> </head> <body> <form id= "Form1" runat= "Server" > <div> <asp:gridview id= "GridView1" runat= "server" datakeynames= "ID number" datasourceid= "SqlDataSource1" allowsorting= "True" backcolor= "white" bordercolor= "#CCCCCC" borderstyle= "None" borderwidth= "1px" cellpadding= "3" Font-size= "12px" onrowdatabound= "GridView1_RowDataBound" > <Columns> <asp:boundfield datafield= "ID card number" headertext= "ID number" readonly= "True" sortexpression= "ID number"/> <asp:boundfield datafield= "name" headertext= "name" sortexpression= "name"/> <asp:boundfield datafield= "Home Address" headertext= "Home Address" sortexpression= "Home Address"/> <asp: BoundField datafield= "headertext=" Postal Code "sortexpression="/> </Columns> <footerstyle backcolor= "White" forecolor= "#000066"/> <rowstyle forecolor= "#000066"/> <selectedrOwstyle backcolor= "#669999" font-bold= "True" forecolor= "white"/> <pagerstyle backcolor= "white" forecolor= "# 000066 "horizontalalign=" left "/> <headerstyle backcolor=" #006699 "font-bold=" True "forecolor=" white "/> < /asp:gridview> <asp:sqldatasource id= "SqlDataSource1" runat= "Server" connectionstring= "<%$ ConnectionStrings: North wind trade ConnectionString1%> "selectcommand=" SELECT top 5 [ID card number], [name], [employee sex], [home address], [postal code] from [Flying fox Studio] "datasourcemode=" DataReader "></asp:SqlDataSource> </div> </form> </body> </html
>
7. Change the background color of the row when you move the mouse to a certain row of the GridView method two:
Effect Chart:
Practice: The same as the above is the code is different
protected void GridView1_RowDataBound (object sender, GridViewRowEventArgs e)
{
//int i;
Perform loops to ensure that each piece of data can be updated
//for (i = 0; i < GridView1.Rows.Count; i++)//{/////
First determine if the data row
//if (E. Row.rowtype = = Datacontrolrowtype.datarow)//{/////
{
change background color//E.row.attributes.add when mouse hovers
(" onMouseOver "," c=this.style.backgroundcolor;this.style.backgroundcolor= ' #00A9FF ' ");
Restores background color
//E.row.attributes.add ("onmouseout", "This.style.backgroundcolor=c") when the mouse moves away;
////
if the bound data row if
(E.row.rowtype = = Datacontrolrowtype.datarow)
{
//mouse passes, the row background color changes
E.row.attributes.add ("onmouseover", "This.style.backgroundcolor= ' #E6F5FA");
When the mouse moves out, the row background color changes
e.row.attributes.add ("onmouseout", "This.style.backgroundcolor= ' #FFFFFF");
}
8.GridView Implement Delete pop-up confirmation dialog box:
Effect Chart:
Implementation method:
Double-click the Onrowdatabound event of the GridView;
In the background The GridView1_RowDataBound () method adds code, and the final code looks like this:
protected void GridView1_RowDataBound (object sender, GridViewRowEventArgs e)
{
//if the bound data row if
(E. Row.rowtype = = Datacontrolrowtype.datarow)
{
if (e.row.rowstate = = Datacontrolrowstate.normal | | E. Row.rowstate = = datacontrolrowstate.alternate)
{
(LinkButton) e.row.cells[6]. Controls[0]). Attributes.Add ("onclick", "Javascript:return Confirm" (' You are sure to delete:/"" + e.row.cells[1]. Text + "/"? ') ");}
}
9.GridView implementation Automatic numbering:
Effect Chart:
Implementation method:
Double-click the Onrowdatabound event of the GridView;
In the background The GridView1_RowDataBound () method adds code, and the final code looks like this:
protected void GridView1_RowDataBound (object sender, GridViewRowEventArgs e)
{
//if the bound data row if
(E. Row.rowtype = = Datacontrolrowtype.datarow)
{
When////mouse passes, the row background color changes
//e.row.attributes.add ("onmouseover", " This.style.backgroundcolor= ' #E6F5FA ');
When the mouse moves out, the row background color changes
//e.row.attributes.add ("onmouseout", "this.style.backgroundcolor= ' #FFFFFF '");
When there is edit column, avoid error, to add RowState judgment
//if (e.row.rowstate = = Datacontrolrowstate.normal | | e.row.rowstate = datacontrolrowstate.alternate)
//{
//((LinkButton) e.row.cells[6]. Controls[0]). Attributes.Add ("onclick", "Javascript:return Confirm" (' You are sure to delete:/"" + e.row.cells[1]. Text + "/"? ') ");
}
if (e.row.rowindex!=-1)
{
int id = e.row.rowindex + 1;
E.row.cells[0]. Text = ID. ToString ();
}
}
Note that the first column of the front desk should be "numbered" because the first column was "eaten".
<asp:gridview id= "GridView1" runat= "Server" cellpadding= "3" onrowdeleting= "gridview1_rowdeleting" "onrowediting=" Gridview1_rowediting "onrowupdating=" gridview1_rowupdating "onrowcancelingedit=" Gridview1_rowcancelingedit " Backcolor= "White" bordercolor= "#CCCCCC" borderstyle= "None" borderwidth= "1px" font-size= "12px" onrowdatabound= " GridView1_RowDataBound "> <footerstyle backcolor=" White "forecolor=" #000066 "/> <Columns> <asp: BoundField datafield= "ID number" headertext= "readonly=" True "/> <asp:boundfield" name "datafield=" user name "/> <asp:boundfield datafield=" Employee gender "headertext=" gender "/> <asp:boundfield" Home Address "datafield=" Home Address "/> <asp:commandfield headertext=" select "showselectbutton= True"/> <asp:commandfield HeaderText= "edit" Showeditbutton= "true"/> <asp:commandfield headertext= "delete" showdeletebutton= "True"/> </Columns> < RowStyle forecolor= "#000066"/> <selectedrowstyle backcolor= "#669999" font-bold= "True"Forecolor="/> <pagerstyle backcolor= "white" forecolor= "#000066" horizontalalign= "left"/> <
HeaderStyle backcolor= "#006699" font-bold= "True" forecolor= "white"/> </asp:GridView>
10.GridView implement custom time currency such as String format:
Effect Chart:
Figure 1-before formatting
Figure 2-After formatting
Workaround:
in asp.net 2.0, if you want to display a date format in a bound column, and so on, you can't display it in the following way
<asp:boundfield datafield= "CreationDate"
dataformatstring= "{0:m-dd-yyyy}"
headertext= "CreationDate" />
This is mainly because the HTMLEncode property is set to True by default and has been prevented from XSS attacks for security reasons, so there are two ways to resolve
First type:
<asp:gridview id= "GridView1" runat= "Server" >
<columns>
<asp:boundfield datafield= " CreationDate "
dataformatstring=" {0:m-dd-yyyy} "
htmlencode= false"
headertext= "CreationDate"/ >
</columns>
</asp>
Set the HTMLEncode to False
Another workaround is to use the template column
<asp:gridview id= "GRIDVIEW3" runat= "Server" >
<columns>
<asp:templatefield headertext= " CreationDate ">
<edititemtemplate>
<asp:label id=" Label1 "runat=" server "
text= ' <%# eval _r ("CreationDate", "{0:m-dd-yyyy}")%> ' >
</asp>
</edititemtemplate>
< itemtemplate>
<asp:label id= "Label1" runat= "server" text=
' <%# Bind ("CreationDate", "{0: M-DD-YYYY} ")%> ' >
</asp>
</itemtemplate>
</asp>
</columns>
</asp>
Foreground code:
<asp:gridview id= "GridView1" runat= "server" datakeynames= "ID number" datasourceid= "SqlDataSource1" allowsorting= " True "backcolor=" White "bordercolor=" "#CCCCCC" borderstyle= "None" borderwidth= "1px" cellpadding= "3" font-size= "12px" onrowdatabound= "GridView1_RowDataBound" > <Columns> <asp:boundfield datafield= "ID number" headertext= "ID number "Readonly=" True "sortexpression=" ID number "/> <asp:boundfield datafield=" name "headertext=" name "sortexpression=" name "/ > <asp:boundfield datafield= "headertext=" Postal Code "sortexpression=" Postal code "/> <asp:boundfield DataField=" Date of birth "headertext=" sortexpression= "Birth date"/> <asp:boundfield datafield= "Starting salary" headertext= "Starting salary" sortexpression= "Starting salary"/> </Columns> <footerstyle backcolor= "white" forecolor= "#000066"/> <rowstyle forecolor= "# 000066 "/> <selectedrowstyle backcolor=" #669999 "font-bold=" True "forecolor=" white "/> <pagerstyle Backcolor= "White" forecolor= "#000066" horizontalalign= "left"/> <headerstylE backcolor= "#006699" font-bold= "True" forecolor= "white"/> </asp:GridView> <asp:sqldatasource id= "
SqlDataSource1 "runat=" Server "connectionstring=" <%$ connectionstrings: North wind trade ConnectionString1%> " Selectcommand= "SELECT Top 5 [date of birth], [starting salary], [ID card number], [name], [home address], [postal code] from [Flying Fox Studio]" datasourcemode= "DataReader" ><
/asp:sqldatasource>
Appendix-Common Format formula:
{0:C} currency;
{0:d4} displays integers in 4-character wide fields filled with 0;
{0:000.0} Rounding decimal points to retain the first few valid digits;
{0:N2} keeps 2 digits of valid digits; {0:n2}% decimal point retains 2 digits valid number plus percent sign;
{0:d} long date; {0:d} short date; {0:YY-MM-DD} for example, 07-3-25;;{0:YYYY-MM-DD} for example, 2007-3-25
3 wonderful topics to be attached:
Asp. NET Control Usage Manual
Asp. NET data-bound controls using totals
Asp. NET controls use totals
The above is the asp.net in the GridView control of a variety of ways to use the first, the content is very rich, and then there are final articles, I hope you do not miss.