A flexible grasp of the various uses of the GridView control in asp.net (top) _ Practical skills

Source: Internet
Author: User
Tags string format connectionstrings

There are many ways to use the GridView control in ASP.net, the content of this article is very rich, I hope we can have some harvest.

1.GridView No code paging sort:

Effect Chart:

Small tip:

1.AllowSorting set to True,aspx code is allowsorting= "True";
2. Default 1 page 10, if you want to modify the number of pages, modify pagesize can be, in the ASPX code is pagesize= "12".
3. The default is one-way sorting, right-click the GridView Pop-up "Properties", select AllowSorting to True.

2.GridView selected, edited, Canceled, deleted:

Effect Chart:

Background code:
You can use SqlHelper, this article is useless. The code is as follows:

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 _default:system.web.ui.page {SqlConnection sqlcon;
SqlCommand sqlcom; String Strcon = "Data source= (local);D atabase= database name; uid= account number;
pwd= password "; protected void Page_Load (object sender, EventArgs e) {if (!
IsPostBack) {bind ();}} protected void Gridview1_rowediting (object sender, GridViewEditEventArgs e) {gridview1.editindex = E.neweditindex; bind
(); }//delete protected void Gridview1_rowdeleting (object sender, Gridviewdeleteeventargs e) {string sqlstr = "Delete from table wh Ere id= ' "+ gridview1.datakeys[e.rowindex].
Value.tostring () + "'";
Sqlcon = new SqlConnection (Strcon);
sqlcom = new SqlCommand (Sqlstr,sqlcon); Sqlcon.
Open (); Sqlcom.
ExecuteNonQuery (); Sqlcon.
Close ();
Bind (); }//update protected void GridView1_rowupdating (object sender, Gridviewupdateeventargs e) {sqlcon = new SqlConnection (Strcon); string sqlstr = "Update table set Field 1= ' "+ (TextBox) (Gridview1.rows[e.rowindex]. CELLS[1]. Controls[0]). Text.tostring (). Trim () + "', field 2= '" + (TextBox) (Gridview1.rows[e.rowindex]. CELLS[2]. Controls[0]). Text.tostring (). Trim () + "', field 3 = '" + (TextBox) (Gridview1.rows[e.rowindex). CELLS[3]. Controls[0]). Text.tostring (). Trim () + "' Where id= '" + gridview1.datakeys[e.rowindex].
Value.tostring () + "'";
Sqlcom=new SqlCommand (Sqlstr,sqlcon); Sqlcon.
Open (); Sqlcom.
ExecuteNonQuery (); Sqlcon.
Close ();
Gridview1.editindex =-1;
Bind (); }//Cancel protected void Gridview1_rowcancelingedit (object sender, Gridviewcancelediteventargs e) {gridview1.editindex =-1
;
Bind ();
}//Binding public void bind () {String sqlstr = ' select * from table '; Sqlcon = new SqlConnection (Strcon);
SqlDataAdapter Myda = new SqlDataAdapter (Sqlstr, Sqlcon);
DataSet myds = new DataSet (); Sqlcon.
Open (); Myda.
Fill (myds, "table"); Gridview1.datasource = myds;
Gridview1.datakeynames = new string[] {"id"};//primary key Gridview1.databind (); Sqlcon.
Close (); 

 }
}

foreground main code:

... <asp:gridview id= "GridView1" runat= "Server" cellpadding= "4" forecolor= "#333333" gridlines= "None" onrowdeleting= "gridview1_rowdeleting" onrowediting= "gridview1_rowediting" onrowupdating= "GridView1_RowUpdating" onrowcancelingedit= "Gridview1_rowcancelingedit" > <footerstyle backcolor= "#990000" font-bold= "True" ForeColor = "White"/> <Columns> <asp:boundfield datafield= "ID number" headertext= "User ID" readonly= "True"/> <asp: BoundField datafield= "name" headertext= "user name"/> <asp:boundfield datafield= "Employee Sex" headertext= "gender"/> <asp: BoundField datafield= "Home Address" headertext= "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= "white"/> <pagerstyle backcolor= "white" forecolor= "#000066 "horizontalalign=" left "/> <headerstyle backcolor=" #006699 "font-bold=" True "forecolor=" white "/> </asp:GridView>

3.GridView positive and negative bidirectional ordering:
effect Picture: Point Name each 2 times of the order, point other also can.

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 Default3:System.Web.UI.Page {SqlConnection sqlcon; string strcon = ' Data source= (local);D atabase= North wind trade; Uid=sa;
Pwd= "; protected void Page_Load (object sender, EventArgs e) {if (!
IsPostBack) {viewstate["SortOrder"] = "identity card number";
viewstate["Orderdire"] = "ASC";
Bind (); } protected void Gridview1_sorting (object sender, GridViewSortEventArgs e) {string spage = E.sortexpression; if (Viewst ate["SortOrder"]. ToString () = = Spage) {if (viewstate["Orderdire"].
ToString () = = "Desc") viewstate["orderdire"] = "ASC";
else viewstate["orderdire"] = "Desc";
else {viewstate["SortOrder"] = e.sortexpression;} bind (); public void Bind () {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"); DataView view = myDS. Tables["Flying Fox Studio"].
DefaultView;
String sort = (string) viewstate["SortOrder"] + "" + (String) viewstate["Orderdire"]; View.
sort = sort;
Gridview1.datasource = view;
Gridview1.databind (); Sqlcon.
Close (); }
}

foreground main code:

 <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 &lt;= 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:

&lt;asp:gridview id= "GridView1" runat= "Server" allowsorting= "True" cellpadding= "3" font-size= "9pt" backcolor= "white" Bordercolor= "#CCCCCC" borderstyle= "None" borderwidth= "1px" &gt; &lt;footerstyle backcolor= "White" forecolor= "#000066 "/&gt; &lt;Columns&gt; &lt;asp:boundfield datafield=" ID number headertext= "User ID" sortexpression= "ID number"/&gt; &lt;asp:
BoundField datafield= "name" headertext= "User name" sortexpression= "name"/&gt; &lt;asp:templatefield "employee Sex" &gt; &lt;ItemTemplate&gt; &lt;asp:dropdownlist id= "DropDownList1" runat= "server" Datasource= ' &lt;%# ddlbind ()%&gt; ' datavaluefield= "Employee Sex" datatextfield= "employee Sex" &gt; &lt;/asp:DropDownList&gt; &lt;/ItemTemplate&gt; &lt;/asp:
templatefield&gt; &lt;asp:boundfield datafield= "Home Address" headertext= "Home Address" sortexpression= "Home Address"/&gt; &lt;/Columns&gt; &lt;rowstyle forecolor= "#000066"/&gt; &lt;selectedrowstyle backcolor= "#669999" font-bold= "True" forecolor= "white"/ &gt; &lt;pagerstyle backcolor= "White" forecolor= "#000066" horizontalalign= "left"/&gt; &lT HeaderStyle backcolor= "#006699" font-bold= "True" forecolor= "white"/&gt; &lt;/asp:GridView&gt;

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 &lt;= 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 &lt;= 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 &lt;= 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:

&lt;asp:gridview id= "GridView1" runat= "Server" allowsorting= "True" cellpadding= "3" font-size= "9pt" backcolor= "white" Bordercolor= "#CCCCCC" borderstyle= "None" borderwidth= "1px" &gt; &lt;footerstyle backcolor= "White" forecolor= "#000066 "/&gt; &lt;Columns&gt; &lt;asp:TemplateField&gt; &lt;ItemTemplate&gt; &lt;asp:checkbox id=" CheckBox1 "runat=" Server " /&gt; &lt;/ItemTemplate&gt; &lt;/asp:TemplateField&gt; &lt;asp:boundfield datafield= "ID number" headertext= "User ID" sortexpression= "ID number"/&gt; &lt;asp:boundfield datafield= "name" headertext= "User name" sortexpression= "name"/&gt; &lt;asp: BoundField datafield= "Home Address" headertext= "Home Address" sortexpression= "Home Address"/&gt; &lt;/Columns&gt; &lt;rowstyle forecolor= "# 000066 "/&gt; &lt;selectedrowstyle backcolor=" #669999 "font-bold=" True "forecolor=" white "/&gt; &lt;pagerstyle Backcolor= "White" forecolor= "#000066" horizontalalign= "left"/&gt; &lt;headerstyle backcolor= "#006699" font-bold= " True "forecolor=" white "/&gt; &lt;/asp:GridView&gt; &lt;asp:checkbox id=" CheckBox2"runat=" Server "font-size=" 9pt "oncheckedchanged=" checkbox2_checkedchanged "text=" Select All "/&gt; &lt;asp:button ID=" Button1 "runat=" Server "font-size=" 9pt "text=" Cancel "onclick=" Button1_Click "/&gt; &lt;asp:button id=" Button2 "runat="

 Server "font-size=" 9pt "text=" "Delete" onclick= "button2_click"/&gt;

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:

&lt;html xmlns= "http://www.w3.org/1999/xhtml" &gt; &lt;head runat= "Server" &gt; &lt;title&gt; implementing the row background color of the GridView
Qingqing Moon http://blog.csdn.net/21aspnet &lt;/title&gt; &lt;/head&gt; &lt;body&gt; &lt;form id= "Form1" runat= "Server" &gt; &lt;div&gt; &lt;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" &gt; &lt;Columns&gt; &lt;asp:boundfield datafield= "ID card number" headertext= "ID number" readonly= "True" sortexpression= "ID number"/&gt; &lt;asp:boundfield datafield= "name" headertext= "name" sortexpression= "name"/&gt; &lt;asp:boundfield datafield= "Home Address" headertext= "Home Address" sortexpression= "Home Address"/&gt; &lt;asp: BoundField datafield= "headertext=" Postal Code "sortexpression="/&gt; &lt;/Columns&gt; &lt;footerstyle backcolor= "White" forecolor= "#000066"/&gt; &lt;rowstyle forecolor= "#000066"/&gt; &lt;selectedrOwstyle backcolor= "#669999" font-bold= "True" forecolor= "white"/&gt; &lt;pagerstyle backcolor= "white" forecolor= "# 000066 "horizontalalign=" left "/&gt; &lt;headerstyle backcolor=" #006699 "font-bold=" True "forecolor=" white "/&gt; &lt; /asp:gridview&gt; &lt;asp:sqldatasource id= "SqlDataSource1" runat= "Server" connectionstring= "&lt;%$ ConnectionStrings: North wind trade ConnectionString1%&gt; "selectcommand=" SELECT top 5 [ID card number], [name], [employee sex], [home address], [postal code] from [Flying fox Studio] "datasourcemode=" DataReader "&gt;&lt;/asp:SqlDataSource&gt; &lt;/div&gt; &lt;/form&gt; &lt;/body&gt; &lt;/html

 &gt;

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".

&lt;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 "&gt; &lt;footerstyle backcolor=" White "forecolor=" #000066 "/&gt; &lt;Columns&gt; &lt;asp: BoundField datafield= "ID number" headertext= "readonly=" True "/&gt; &lt;asp:boundfield" name "datafield=" user name "/&gt; &lt;asp:boundfield datafield=" Employee gender "headertext=" gender "/&gt; &lt;asp:boundfield" Home Address "datafield=" Home Address "/&gt; &lt;asp:commandfield headertext=" select "showselectbutton= True"/&gt; &lt;asp:commandfield HeaderText= "edit" Showeditbutton= "true"/&gt; &lt;asp:commandfield headertext= "delete" showdeletebutton= "True"/&gt; &lt;/Columns&gt; &lt; RowStyle forecolor= "#000066"/&gt; &lt;selectedrowstyle backcolor= "#669999" font-bold= "True"Forecolor="/&gt; &lt;pagerstyle backcolor= "white" forecolor= "#000066" horizontalalign= "left"/&gt; &lt;
 HeaderStyle backcolor= "#006699" font-bold= "True" forecolor= "white"/&gt; &lt;/asp:GridView&gt;

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:

&lt;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" &gt; &lt;Columns&gt; &lt;asp:boundfield datafield= "ID number" headertext= "ID number "Readonly=" True "sortexpression=" ID number "/&gt; &lt;asp:boundfield datafield=" name "headertext=" name "sortexpression=" name "/ &gt; &lt;asp:boundfield datafield= "headertext=" Postal Code "sortexpression=" Postal code "/&gt; &lt;asp:boundfield DataField=" Date of birth "headertext=" sortexpression= "Birth date"/&gt; &lt;asp:boundfield datafield= "Starting salary" headertext= "Starting salary" sortexpression= "Starting salary"/&gt; &lt;/Columns&gt; &lt;footerstyle backcolor= "white" forecolor= "#000066"/&gt; &lt;rowstyle forecolor= "# 000066 "/&gt; &lt;selectedrowstyle backcolor=" #669999 "font-bold=" True "forecolor=" white "/&gt; &lt;pagerstyle Backcolor= "White" forecolor= "#000066" horizontalalign= "left"/&gt; &lt;headerstylE backcolor= "#006699" font-bold= "True" forecolor= "white"/&gt; &lt;/asp:GridView&gt; &lt;asp:sqldatasource id= "
SqlDataSource1 "runat=" Server "connectionstring=" &lt;%$ connectionstrings: North wind trade ConnectionString1%&gt; " Selectcommand= "SELECT Top 5 [date of birth], [starting salary], [ID card number], [name], [home address], [postal code] from [Flying Fox Studio]" datasourcemode= "DataReader" &gt;&lt;
 /asp:sqldatasource&gt;

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.

Related Article

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.