天天看點

asp.net GridView用法介紹

GridView??浠g????椤墊??搴?

GridView??涓?锛?缂?杈?锛???娑?锛????? GridView姝e????????搴?

GridView??涓???????DropDownList缁???

GridView??CheckBox缁???

榧???绉誨??ridView??涓?琛??舵?瑰??璇ヨ????????叉?規?涓?

榧???绉誨??ridView??涓?琛??舵?瑰??璇ヨ????????叉?規?浜?

GridView瀹??闆???ゆ?跺脊?虹‘璁ゅ?矽??妗?

GridView瀹??拌???ㄧ??? GridView瀹??拌??瀹?涔??堕?磋揣甯?绛?瀛?绗?覆?煎?

GridView瀹??扮?ㄢ??...??浠f?胯??垮??绗?覆

GridView涓????㈣?涓?寮哄?舵?㈣?

GridView?劇ず??????涓???

GridView寮瑰?烘?伴〉??寮瑰?烘?扮???? GridView?哄??琛ㄥご(涓???avascript????SS锛?2琛?浠g??锛?寰?濂界??

GridView??骞惰〃澶村???琛ㄥご????瀹?缇???锛?浠ュ??骞???3琛?涓句?锛?

GridView绐??烘?劇ず??涓??????鹼?渚?濡???棰?浣?浜?澶?灏?锛????頒????肩??锛?

GridView???ヨ???ㄦ???姹?骞沖???煎?璁? GridView?版??瀵煎??xcel/Excel?版??璇誨??ridView

1.GridView??浠g????椤墊??搴?锛?

?????撅?

asp.net GridView用法介紹

1.AllowSorting璁句負True锛?aspx浠g??涓???AllowSorting="True"锛?

2.榛?璁?椤?0?★?濡???瑕?淇??規??椤墊?℃?幫?淇???ageSize?沖??锛???spx浠g??涓???PageSize="12"??

3.榛?璁ょ??????????搴???锛??沖??ridView寮瑰?衡??灞??р??锛?????llowSorting涓?rue?沖????

2.GridView??涓?锛?缂?杈?锛???娑?锛????わ?

?????撅?

asp.net GridView用法介紹

???頒唬??锛?

浣???浠ヤ嬌??qlhelper,????娌$?ㄣ??浠g??濡?涓?锛?

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

{

//娓?娓?????ttp://blog.csdn.net/21aspnet?

??? SqlConnection sqlcon;

??? SqlCommand sqlcom;

??? string strCon = "Data Source=(local);Database=?版??搴???;Uid=甯???Pwd=瀵???";

??? protected void Page_Load(object sender, EventArgs e)

??? {

??????? if (!IsPostBack)

??????? {

??????????? bind();

??????? }

??? }

??? protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)

??? {

??????? GridView1.EditIndex = e.NewEditIndex;

??????? bind();

??? }

//???? ??? protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)

??? {

??????? string sqlstr = "delete from?琛?where id='" + GridView1.DataKeys[e.RowIndex].Value.ToString() + "'";

??????? sqlcon = new SqlConnection(strCon);

??????? sqlcom = new SqlCommand(sqlstr,sqlcon);

??????? sqlcon.Open();

??????? sqlcom.ExecuteNonQuery();

??????? sqlcon.Close();

??????? bind();

??? }

//?存?? ??? protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)

??? {

??????? sqlcon = new SqlConnection(strCon);

??????? string sqlstr = "update 琛?set 瀛?娈?='"

??????????? + ((TextBox)(GridView1.Rows[e.RowIndex].Cells[1].Controls[0])).Text.ToString().Trim() + "',瀛?娈?='"

??????????? + ((TextBox)(GridView1.Rows[e.RowIndex].Cells[2].Controls[0])).Text.ToString().Trim() + "',瀛?娈?='"

??????????? + ((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();

??? }

//??娑?

??? protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)

??? {

??????? GridView1.EditIndex = -1;

??????? bind();

??? }

//缁?瀹?

??? public void bind()

??? {

??????? string sqlstr = "select * from 琛?;

??????? sqlcon = new SqlConnection(strCon);

??????? SqlDataAdapter myda = new SqlDataAdapter(sqlstr, sqlcon);

??????? DataSet myds = new DataSet();

??????? sqlcon.Open();

??????? myda.Fill(myds, "琛?);

??????? GridView1.DataSource = myds;

??????? GridView1.DataKeyNames = new string[] { "id" };//涓婚??

??????? GridView1.DataBind();

??????? sqlcon.Close();

??? }

}

???頒富瑕?浠g??锛?

??????????????????????????? ... ...

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" 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="韬?浠借???風??" HeaderText="?ㄦ??D" ReadOnly="True" />

??????????????????????????? <asp:BoundField DataField="濮???" HeaderText="?ㄦ?峰???" />

??????????????????????????? <asp:BoundField DataField="??宸ユ?у??" HeaderText="?у??" />

??????????????????????????? <asp:BoundField DataField="瀹跺涵浣???" HeaderText="瀹跺涵浣???" />

??????????????????????????? <asp:CommandField HeaderText="???? ShowSelectButton="True" />

??????????????????????????? <asp:CommandField HeaderText="缂?杈?" ShowEditButton="True" />

??????????????????????????? <asp:CommandField HeaderText="???? 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姝e????????搴?锛?

?????撅??瑰?????2娆$????搴?锛??瑰?朵?涔?涓??峰??浠ャ??

asp.net GridView用法介紹
asp.net GridView用法介紹

???頒唬??锛?

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

{

//娓?娓????跨????瀹?ttp://blog.csdn.net/21aspnet?

??? SqlConnection sqlcon;

??? string strCon = "Data Source=(local);Database=??椋?璐告??;Uid=sa;Pwd=";

??? protected void Page_Load(object sender, EventArgs e)

??? {

??????? if (!IsPostBack)

??????? {

??????????? ViewState["SortOrder"] = "韬?浠借???風??";

??????????? ViewState["OrderDire"] = "ASC";

??????????? bind();

??????? }

??? }

??? protected void GridView1_Sorting(object sender, GridViewSortEventArgs e)

??? {

??????? string sPage = e.SortExpression;

??????? if (ViewState["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 椋???宸ヤ?瀹?;

??????? sqlcon = new SqlConnection(strCon);

??????? SqlDataAdapter myda = new SqlDataAdapter(sqlstr, sqlcon);

??????? DataSet myds = new DataSet();

??????? sqlcon.Open();

??????? myda.Fill(myds, "椋???宸ヤ?瀹?);

??????? DataView view = myds.Tables["椋???宸ヤ?瀹?].DefaultView;

??????? string sort = (string)ViewState["SortOrder"] + " " + (string)ViewState["OrderDire"];

??????? view.Sort = sort;

??????? GridView1.DataSource = view;

??????? GridView1.DataBind();

??????? sqlcon.Close();

??? }

}

???頒富瑕?浠g??锛?

<asp:GridView ID="GridView1" runat="server" AllowSorting="True" AutoGenerateColumns="False"

??????????????????????? 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="韬?浠借???風??" HeaderText="?ㄦ??D" SortExpression="韬?浠借???風??" />

??????????????????????????? <asp:BoundField DataField="濮???" HeaderText="?ㄦ?峰???" SortExpression="濮???"/>

??????????????????????????? <asp:BoundField DataField="??宸ユ?у??" HeaderText="?у??" SortExpression="??宸ユ?у??"/>

??????????????????????????? <asp:BoundField DataField="瀹跺涵浣???" HeaderText="瀹跺涵浣???" SortExpression="瀹跺涵浣???"/>

???????????????????????????????

??????????????????????? </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>

?

4.GridView??涓???????DropDownList缁???锛?

?????撅?

asp.net GridView用法介紹

???頒唬??锛?

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);Database=??椋?璐告??;Uid=sa;Pwd=sa";

??? protected void Page_Load(object sender, EventArgs e)

??? {

??????? DropDownList ddl;

??????? if (!IsPostBack)

??????? {

??????????? string sqlstr = "select top 5 * from 椋???宸ヤ?瀹?;

??????????? sqlcon = new SqlConnection(strCon);

??????????? SqlDataAdapter myda = new SqlDataAdapter(sqlstr, sqlcon);

??????????? DataSet myds = new DataSet();

??????????? sqlcon.Open();

??????????? myda.Fill(myds, "椋???宸ヤ?瀹?);

??????????? GridView1.DataSource = myds;

??????????? GridView1.DataBind();

??????????? for (int i = 0; i <= GridView1.Rows.Count - 1; i++)

??????????? {

??????????????? DataRowView mydrv = myds.Tables["椋???宸ヤ?瀹?].DefaultView[i];

??????????????? if (Convert.ToString(mydrv["??宸ユ?у??"]).Trim() == "True")

??????????????? {

??????????????????? ddl = (DropDownList)GridView1.Rows[i].FindControl("DropDownList1");

??????????????????? ddl.SelectedIndex = 0;

??????????????? }

??????????????? if (Convert.ToString(mydrv["??宸ユ?у??"]).Trim() == "False")

??????????????? {

??????????????????? ddl = (DropDownList)GridView1.Rows[i].FindControl("DropDownList1");

??????????????????? ddl.SelectedIndex = 1;

??????????????? }

??????????? }

??????????? sqlcon.Close();

??????? }

??? }

??? public SqlDataReader ddlbind()

??? {

??????? string sqlstr = "select distinct ??宸ユ?у?? from 椋???宸ヤ?瀹?;

??????? sqlcon = new SqlConnection(strCon);

??????? SqlCommand sqlcom = new SqlCommand(sqlstr, sqlcon);

??????? sqlcon.Open();

??????? return sqlcom.ExecuteReader();

??? }

???頒富瑕?浠g??锛?

<asp:GridView ID="GridView1" runat="server" AllowSorting="True" AutoGenerateColumns="False"

??????????????????????? CellPadding="3" Font-Size="9pt"? BackColor="White" BorderColor="#CCCCCC" BorderStyle="None" BorderWidth="1px">

??????????????????????? <FooterStyle BackColor="White" ForeColor="#000066" />

??????????????????????? <Columns>

???????????????????????????? <asp:BoundField DataField="韬?浠借???風??" HeaderText="?ㄦ??D" SortExpression="韬?浠借???風??" />

??????????????????????????? <asp:BoundField DataField="濮???" HeaderText="?ㄦ?峰???" SortExpression="濮???"/>

??????????????????????????? <asp:TemplateField HeaderText="??宸ユ?у??">

??????????????????????????????? <ItemTemplate>

??????????????????????????????????? <asp:DropDownList ID="DropDownList1" runat="server" DataSource='<%# ddlbind()%>' DataValueField="??宸ユ?у??" DataTextField="??宸ユ?у??">

??????????????????????????????????? </asp:DropDownList>

??????????????????????????????? </ItemTemplate>

??????????????????????????? </asp:TemplateField>

??????????????????????????? <asp:BoundField DataField="瀹跺涵浣???" HeaderText="瀹跺涵浣???" SortExpression="瀹跺涵浣???"/>

???????????????????????????????

??????????????????????? </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>

5.GridView??CheckBox缁???锛?

?????撅?

asp.net GridView用法介紹

???頒唬??锛?

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

{

//娓?娓?????ttp://blog.csdn.net/21aspnet

??? SqlConnection sqlcon;

??? string strCon = "Data Source=(local);Database=??椋?璐告??;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 椋???宸ヤ?瀹?where 韬?浠借???風??='" + 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 * from 椋???宸ヤ?瀹?;

??????? 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[] { "韬?浠借???風??" };

??????? GridView1.DataBind();

??????? sqlcon.Close();

??? }

}

???頒富瑕?浠g??锛?

<asp:GridView ID="GridView1" runat="server" AllowSorting="True" AutoGenerateColumns="False"

??????????????????????? 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="韬?浠借???風??" HeaderText="?ㄦ??D" SortExpression="韬?浠借???風??" />

??????????????????????????? <asp:BoundField DataField="濮???" HeaderText="?ㄦ?峰???" SortExpression="濮???"/>

???????????????????????????

??????????????????????????? <asp:BoundField DataField="瀹跺涵浣???" HeaderText="瀹跺涵浣???" SortExpression="瀹跺涵浣???"/>

???????????????????????????????

??????????????????????? </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" AutoPostBack="True" Font-Size="9pt" OnCheckedChanged="CheckBox2_CheckedChanged"

??????????????????????? Text="?ㄩ??" />

??????????????????? <asp:Button ID="Button1" runat="server" Font-Size="9pt" Text="??娑?" OnClick="Button1_Click" />

??????????????????? <asp:Button ID="Button2" runat="server" Font-Size="9pt" Text="???? OnClick="Button2_Click" />

6.榧???绉誨??ridView??涓?琛??舵?瑰??璇ヨ????????叉?規?涓?锛?

?????撅?

asp.net GridView用法介紹

??娉?锛?

????ridView??OnRowDataBound浜?浠訛?

?ㄥ???扮??GridView1_RowDataBound()?規?娣誨??浠g??锛?????浠g??濡?涓???绀猴?

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)

??? {

??????? int i;

??????? //?ц?寰???锛?淇?璇?姣??℃?版???藉??浠ユ?存?? ??????? for (i = 0; i < GridView1.Rows.Count; i++)

??????? {

??????????? //棣????ゆ?????????版??琛?

??????????? if (e.Row.RowType == DataControlRowType.DataRow)

??????????? {

??????????????? //褰?榧????????舵?存?矽?????? ??????????????? e.Row.Attributes.Add("onmouseover", "c=this.style.backgroundColor;this.style.backgroundColor='#00A9FF'");

??????????????? //褰?榧???绉誨??惰????????? ??????????????? e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor=c");

??????????? }

??????? }

??? }

???頒唬??锛?

<html xmlns="http://www.w3.org/1999/xhtml" >

<head runat="server">

??? <title>瀹??伴?????杩??瑰??GridView??琛???????娓?娓?????ttp://blog.csdn.net/21aspnet?</title>

</head>

<body>

??? <form id="form1" runat="server">

??? <div>

??????? <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataKeyNames="韬?浠借???風??"

??????????? DataSourceID="SqlDataSource1" AllowSorting="True" BackColor="White" BorderColor="#CCCCCC" BorderStyle="None" BorderWidth="1px" CellPadding="3" Font-Size="12px" OnRowDataBound="GridView1_RowDataBound">

??????????? <Columns>

??????????????? <asp:BoundField DataField="韬?浠借???風??" HeaderText="韬?浠借???風??" ReadOnly="True" SortExpression="韬?浠借???風??" />

??????????????? <asp:BoundField DataField="濮???" HeaderText="濮???" SortExpression="濮???" />

??????????????? <asp:BoundField DataField="瀹跺涵浣???" HeaderText="瀹跺涵浣???" SortExpression="瀹跺涵浣???" />

??????????????? <asp:BoundField DataField="???跨???" HeaderText="???跨???" 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:??椋?璐告??ConnectionString1 %>"

??????????? SelectCommand="SELECT top 5 [韬?浠借???風??], [濮???], [??宸ユ?у??], [瀹跺涵浣???], [???跨???] FROM [椋???宸ヤ?瀹?" DataSourceMode="DataReader"></asp:SqlDataSource>

???

??? </div>

??? </form>

</body>

</html>

7.榧???绉誨??ridView??涓?琛??舵?瑰??璇ヨ????????叉?規?浜?锛?

?????撅?

asp.net GridView用法介紹

??娉?锛???涓??㈢??涓??峰氨??浠g??涓???

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)

??? {

??????? //int i;

??????? ?ц?寰???锛?淇?璇?姣??℃?版???藉??浠ユ?存?? ??????? //for (i = 0; i < GridView1.Rows.Count; i++)

??????? //{

??????? //??? //棣????ゆ?????????版??琛?

??????? //??? if (e.Row.RowType == DataControlRowType.DataRow)

??????? //??? {

??????? //??????? //褰?榧????????舵?存?矽?????? ??????? //??????? e.Row.Attributes.Add("onmouseover", "c=this.style.backgroundColor;this.style.backgroundColor='#00A9FF'");

??????? //??????? //褰?榧???绉誨??惰????????? ??????? //??????? e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor=c");

??????? //??? }

??????? //}

??????? //濡?????缁?瀹??版??琛?

??????? if (e.Row.RowType == DataControlRowType.DataRow)

??????? {

??????????? //榧???缁?杩??訛?琛??????插??

??????????? e.Row.Attributes.Add("onmouseover", "this.style.backgroundColor='#E6F5FA'");

??????????? //榧???绉誨?烘?訛?琛??????插??

??????????? e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor='#FFFFFF'");

??????? }

??? }

8.GridView瀹??闆???ゆ?跺脊?虹‘璁ゅ?矽??妗?锛?

?????撅?

asp.net GridView用法介紹

瀹??版?規?锛?

????ridView??OnRowDataBound浜?浠訛?

?ㄥ???扮??GridView1_RowDataBound()?規?娣誨??浠g??锛?????浠g??濡?涓???绀猴?

??? protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)

??? {

??????? //濡?????缁?瀹??版??琛?

??????? 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('浣?纭?璁よ????わ?\"" + e.Row.Cells[1].Text + "\"???')");

??????????? }

??????? }

??? }

9.GridView瀹??拌???ㄧ??鳳?

?????撅?

asp.net GridView用法介紹

瀹??版?規?锛?

????ridView??OnRowDataBound浜?浠訛?

?ㄥ???扮??GridView1_RowDataBound()?規?娣誨??浠g??锛?????浠g??濡?涓???绀猴?

??? protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)

??? {

??????? //濡?????缁?瀹??版??琛? //娓?娓?????ttp://blog.csdn.net/21aspnet?

??????? if (e.Row.RowType == DataControlRowType.DataRow)

??????? {

??????????? 榧???缁?杩??訛?琛??????插??

??????????? //e.Row.Attributes.Add("onmouseover", "this.style.backgroundColor='#E6F5FA'");

??????????? 榧???绉誨?烘?訛?琛??????插??

??????????? //e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor='#FFFFFF'");

??????????? 褰???缂?杈????訛??垮???洪??锛?瑕?????RowState?ゆ??

??????????? //if (e.Row.RowState == DataControlRowState.Normal || e.Row.RowState == DataControlRowState.Alternate)

??????????? //{

??????????? //??? ((LinkButton)e.Row.Cells[6].Controls[0]).Attributes.Add("onclick", "javascript:return confirm('浣?纭?璁よ????わ?\"" + e.Row.Cells[1].Text + "\"???')");

??????????? //}

??????? }

??????? if (e.Row.RowIndex != -1)

??????? {

??????????? int id = e.Row.RowIndex + 1;

??????????? e.Row.Cells[0].Text = id.ToString();

??????? }

??? }

?

娉ㄦ??杩??舵??濂芥?????扮??绗?涓?????琛ㄥご璇ヤ負??缂??封??锛???涓轟互????绗?涓???琚?????????浜???

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" 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="韬?浠借???風??" HeaderText="缂??? ReadOnly="True" />

??????????????????????????? <asp:BoundField DataField="濮???" HeaderText="?ㄦ?峰???" />

??????????????????????????? <asp:BoundField DataField="??宸ユ?у??" HeaderText="?у??" />

??????????????????????????? <asp:BoundField DataField="瀹跺涵浣???" HeaderText="瀹跺涵浣???" />

??????????????????????????? <asp:CommandField HeaderText="???? ShowSelectButton="True" />

??????????????????????????? <asp:CommandField HeaderText="缂?杈?" ShowEditButton="True" />

??????????????????????????? <asp:CommandField HeaderText="???? 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>

10.GridView瀹??拌??瀹?涔??堕?磋揣甯?绛?瀛?绗?覆?煎?锛?

?????撅?

??-???煎?????

asp.net GridView用法介紹

??-?煎?????

asp.net GridView用法介紹

瑙e?蟲?規?锛?

??sp.net 2.0涓?锛?濡???瑕??ㄧ?瀹???涓??劇ず姣?濡??ユ???煎?绛?锛?濡????ㄤ??㈢???規????劇ず涓?浜???

<asp :BoundField DataField="CreationDate"

DataFormatString="{0:M-dd-yyyy}"

HeaderText="CreationDate" />

涓昏????變?htmlencode灞??ч?璁よ?劇疆涓?rue,宸查?叉??SS?誨?夥?瀹??ㄨ搗瑙????ㄧ??锛???浠ワ???浠ユ??浠ヤ?涓ょ??規?瑙e??

1??

<asp :GridView ID="GridView1" runat="server">

<columns>

<asp :BoundField DataField="CreationDate"

DataFormatString="{0:M-dd-yyyy}"

HtmlEncode="false"

HeaderText="CreationDate" />

</columns>

</asp>

灏?htmlencode璁劇疆涓?alse?沖??

?????瑙e?蟲?規?涓猴?浣跨?ㄦā????

<asp :GridView ID="GridView3" runat="server" >

<columns>

<asp :TemplateField HeaderText="CreationDate" >

<edititemtemplate>

<asp :Label ID="Label1" runat="server"

Text='<%# Eval("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>

???頒唬??锛?

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataKeyNames="韬?浠借???風??"

??????????? DataSourceID="SqlDataSource1" AllowSorting="True" BackColor="White" BorderColor="#CCCCCC" BorderStyle="None" BorderWidth="1px" CellPadding="3" Font-Size="12px" OnRowDataBound="GridView1_RowDataBound">

??????????? <Columns>

??????????????? <asp:BoundField DataField="韬?浠借???風??" HeaderText="韬?浠借???風??" ReadOnly="True" SortExpression="韬?浠借???風??" />

??????????????? <asp:BoundField DataField="濮???" HeaderText="濮???" SortExpression="濮???" />

??????????????? <asp:BoundField DataField="???跨???" HeaderText="???跨???" SortExpression="???跨???" />

??????????????? <asp:BoundField DataField="?虹???ユ??" HeaderText="?虹???ユ??" SortExpression="?虹???ユ??" />

??????????????? <asp:BoundField DataField="璧瘋??" HeaderText="璧瘋??" 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:??椋?璐告??ConnectionString1 %>"

??????????? SelectCommand="SELECT top 5 [?虹???ユ??], [璧瘋??], [韬?浠借???風??], [濮???], [瀹跺涵浣???], [???跨???] FROM [椋???宸ヤ?瀹?" DataSourceMode="DataReader"></asp:SqlDataSource>

??褰?-甯哥?ㄦ?煎?????寮?锛?

{0:C}? 璐у?锛?

{0:D4}??濉?????4涓?瀛?绗??界??瀛?娈典腑?劇ず?存?幫?

{0:000.0}????浜??ュ??扮?逛???绗???浣??????闆??锛?

{0:N2}灏??扮?逛???2浣??????闆??锛?{0:N2}%?? 灏??扮?逛???2浣??????闆?????懼???鳳?

{0:D}?挎?ユ??锛?{0:d}???ユ??锛?{0:yy-MM-dd}?? 渚?濡?07-3-25锛?锛?{0:yyyy-MM-dd}? 渚?濡?2007-3-25

?11.GridView瀹??扮?ㄢ??...??浠f?胯??垮??绗?覆锛?

?????撅?

asp.net GridView用法介紹

瑙e?蟲?規?锛??版??缁?瀹???杩?婊ゆ??涓?琛??沖??

for (int i = 0; i <= GridView1.Rows.Count - 1; i++)

??????? {

??????????? DataRowView mydrv;

??????????? string gIntro;

??????????? if (GridView1.PageIndex == 0)

??????????? {

??????????????? mydrv = myds.Tables["椋???宸ヤ?瀹?].DefaultView[i];//琛ㄥ??

??????????????? gIntro = Convert.ToString(mydrv["瀹跺涵浣???"]);//??瑕?澶?????瀛?娈? ??????????????? GridView1.Rows[i].Cells[3].Text = SubStr(gIntro, 2);

??????????? }

??????????? else

??????????? {

??????????????? mydrv = myds.Tables["椋???宸ヤ?瀹?].DefaultView[i + (5 * GridView1.PageIndex)];

??????????????? gIntro = Convert.ToString(mydrv["瀹跺涵浣???"]);

??????????????? GridView1.Rows[i].Cells[3].Text = SubStr(gIntro, 2);

??????????? }

??????? }?

璋??ㄧ???規?锛?

??? public string SubStr(string sString, int nLeng)

??? {

??????? if (sString.Length <= nLeng)

??????? {

??????????? return sString;

??????? }

??????? string sNewStr = sString.Substring(0, nLeng);

??????? sNewStr = sNewStr + "...";

??????? return sNewStr;

??? }

???闆?ㄩ?ㄤ唬??锛?

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);Database=??椋?璐告??;Uid=sa;Pwd=sa";

??? protected void Page_Load(object sender, EventArgs e)

??? {

??????? if (!IsPostBack)

??????? {

??????????? ViewState["SortOrder"] = "韬?浠借???風??";

??????????? ViewState["OrderDire"] = "ASC";

??????????? bind();

??????? }

??? }

??? protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)

??? {

??????? GridView1.EditIndex = e.NewEditIndex;

??????? bind();

??? }

??? protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)

??? {

??????? string sqlstr = "delete from 椋???宸ヤ?瀹?where 韬?浠借???風??='" + GridView1.DataKeys[e.RowIndex].Value.ToString() + "'";

??????? sqlcon = new SqlConnection(strCon);

??????? sqlcom = new SqlCommand(sqlstr,sqlcon);

??????? sqlcon.Open();

??????? sqlcom.ExecuteNonQuery();

??????? sqlcon.Close();

??????? bind();

??? }

??? protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)

??? {

??????? sqlcon = new SqlConnection(strCon);

??????? string sqlstr = "update 椋???宸ヤ?瀹?set 濮???='"

??????????? + ((TextBox)(GridView1.Rows[e.RowIndex].Cells[1].Controls[0])).Text.ToString().Trim() + "',瀹跺涵浣???='"

??????????? + ((TextBox)(GridView1.Rows[e.RowIndex].Cells[3].Controls[0])).Text.ToString().Trim() + "' where 韬?浠借???風??='"

??????????? + GridView1.DataKeys[e.RowIndex].Value.ToString() + "'";

??????? sqlcom=new SqlCommand(sqlstr,sqlcon);

??????? sqlcon.Open();

??????? sqlcom.ExecuteNonQuery();

??????? sqlcon.Close();

??????? GridView1.EditIndex = -1;

??????? bind();

??? }

??? protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)

??? {

??????? GridView1.EditIndex = -1;

??????? bind();

??? }

??? public void bind()

??? {

??????? string sqlstr = "select top 5 * from 椋???宸ヤ?瀹?;

??????? sqlcon = new SqlConnection(strCon);

??????? SqlDataAdapter myda = new SqlDataAdapter(sqlstr, sqlcon);

??????? DataSet myds = new DataSet();

??????? sqlcon.Open();

??????? myda.Fill(myds, "椋???宸ヤ?瀹?);

??????? GridView1.DataSource = myds;

??????? GridView1.DataKeyNames = new string[] { "韬?浠借???風??" };

??????? GridView1.DataBind();

??????? for (int i = 0; i <= GridView1.Rows.Count - 1; i++)

??????? {

??????????? DataRowView mydrv;

??????????? string gIntro;

??????????? if (GridView1.PageIndex == 0)

??????????? {

??????????????? mydrv = myds.Tables["椋???宸ヤ?瀹?].DefaultView[i];

??????????????? gIntro = Convert.ToString(mydrv["瀹跺涵浣???"]);

??????????????? GridView1.Rows[i].Cells[3].Text = SubStr(gIntro, 2);

??????????? }

??????????? else

??????????? {

??????????????? mydrv = myds.Tables["椋???宸ヤ?瀹?].DefaultView[i + (5 * GridView1.PageIndex)];

??????????????? gIntro = Convert.ToString(mydrv["瀹跺涵浣???"]);

??????????????? GridView1.Rows[i].Cells[3].Text = SubStr(gIntro, 2);

??????????? }

??????? }

???????

??????? sqlcon.Close();

??? }

??? public string SubStr(string sString, int nLeng)

??? {

??????? if (sString.Length <= nLeng)

??????? {

??????????? return sString;

??????? }

??????? string sNewStr = sString.Substring(0, nLeng);

??????? sNewStr = sNewStr + "...";

??????? return sNewStr;

??? }

??? protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)

??? {

??????? //濡?????缁?瀹??版??琛??

??????? if (e.Row.RowType == DataControlRowType.DataRow)

??????? {

??????????? 榧???缁?杩??訛?琛??????插??

??????????? //e.Row.Attributes.Add("onmouseover", "this.style.backgroundColor='#E6F5FA'");

??????????? 榧???绉誨?烘?訛?琛??????插??

??????????? //e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor='#FFFFFF'");

??????????? 褰???缂?杈????訛??垮???洪??锛?瑕?????RowState?ゆ??

??????????? //if (e.Row.RowState == DataControlRowState.Normal || e.Row.RowState == DataControlRowState.Alternate)

??????????? //{

??????????? //??? ((LinkButton)e.Row.Cells[6].Controls[0]).Attributes.Add("onclick", "javascript:return confirm('浣?纭?璁よ????わ?\"" + e.Row.Cells[1].Text + "\"???')");

??????????? //}

??????? }

??????? if (e.Row.RowIndex != -1)

??????? {

??????????? int id = e.Row.RowIndex + 1;

??????????? e.Row.Cells[0].Text = id.ToString();

??????? }

??? }

}

?11.GridView涓????㈣?涓?寮哄?舵?㈣?锛?

?????撅?

asp.net GridView用法介紹

棣???璁劇疆<asp:BoundField DataField="瀹跺涵浣???" HeaderText="瀹跺涵浣???"? ItemStyle-Width="100" />

gridview????涓???缁?瀹????版??寰??匡??劇ず???跺???ㄤ?琛????㈡?劇ず锛?椤甸?㈡??寰?寰?瀹姐??

??????杩?缁??辨??娈典負涓?涓??翠?瀵艱?寸??锛???owDataBound涓?娣誨??涓?浜?涓???.Row.Cells[2].Style.Add("word-break", "break-all")灏卞??浠ャ??

濡???瑕?缁?????????澧???姝ゅ??э?

?protected void Page_Load(object sender, EventArgs e)

??? {

??????? //姝e父?㈣?

??????? GridView1.Attributes.Add("style", "word-break:keep-all;word-wrap:normal");

??????? //涓??㈣?琛??????ㄦ?㈣?

??????? GridView1.Attributes.Add("style", "word-break:break-all;word-wrap:break-word");

??????? if (!IsPostBack)

??????? {

?????????????bind();//璋??ㄦ?版??缁?瀹??沖??

??????? }

??? }

?諱?锛?????SS??word-break:break-all;word-wrap:break-word灞??у?沖??锛?杩?涓?灞??ф?????ㄧ??瀵逛?椤藉?虹?????㈣???棰??藉??浠ヨВ?籌?涓?灞???浜?GridView??

?12.GridView?劇ず??????涓???锛?

???規?涓烘???跨????锛?涓???浜?缃?涓??朵??瑰?锛???瑙?寰??ㄤ?涓?CheckBox?翠漢?у??锛?杩??峰??浠ラ????涓?蹇?瑕?????锛?璁╃?ㄦ?瘋??宸遍???╅??瑕??虹?扮????锛??ㄥ???澶????惰???涓?涓?寰?濂界??瑙e?蟲?規?锛?

?????撅?

??-寮?濮?

asp.net GridView用法介紹

??-?瑰?繪?劇ず??CheckBox??

asp.net GridView用法介紹

瑙e?蟲?規?锛?

public void bind()

??? {

??????? string sqlstr = "select top 5 * from 椋???宸ヤ?瀹?;

??????? sqlcon = new SqlConnection(strCon);

??????? SqlDataAdapter myda = new SqlDataAdapter(sqlstr, sqlcon);

??????? DataSet myds = new DataSet();

??????? sqlcon.Open();

??????? myda.Fill(myds, "椋???宸ヤ?瀹?);

??????? GridView1.DataSource = myds;

??????? GridView1.DataKeyNames = new string[] { "韬?浠借???風??" };

??????? GridView1.DataBind();

??????? sqlcon.Close();

??????? GridView1.Columns[3].Visible = false;//涓?寮?濮?????

??????? CheckBox1.Checked = false;//濡???涓?杩??峰???㈢??浠g??浼???浠?True

??? }

????heckBox1锛???heckedChanged?規?????涓?浠g??锛?????浠g??濡?涓?锛?

protected void CheckBox1_CheckedChanged(object sender, EventArgs e)

??? {

???????? GridView1.Columns[3].Visible=! GridView1.Columns[3].Visible;

???????? Response.Write("GridView1??绗?4???闆?ㄧ???劇ず?????舵????锛?"+GridView1.Columns[3].Visible.ToString());

??? }

娉ㄦ??锛?CheckBox1??AutoPostBack瑕?True锛?

???闆?ㄩ?ㄤ唬??濡?涓?锛?

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);Database=??椋?璐告??;Uid=sa;Pwd=sa";

??? protected void Page_Load(object sender, EventArgs e)

??? {

??????? if (!IsPostBack)

??????? {

??????????? ViewState["SortOrder"] = "韬?浠借???風??";

??????????? ViewState["OrderDire"] = "ASC";

??????????? bind();

?????????????????? }

??? }

??? protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)

??? {

??????? GridView1.EditIndex = e.NewEditIndex;

??????? bind();

??? }

??? protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)

??? {

??????? string sqlstr = "delete from 椋???宸ヤ?瀹?where 韬?浠借???風??='" + GridView1.DataKeys[e.RowIndex].Value.ToString() + "'";

??????? sqlcon = new SqlConnection(strCon);

??????? sqlcom = new SqlCommand(sqlstr,sqlcon);

??????? sqlcon.Open();

??????? sqlcom.ExecuteNonQuery();

??????? sqlcon.Close();

??????? bind();

??? }

??? protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)

??? {

??????? sqlcon = new SqlConnection(strCon);

??????? string sqlstr = "update 椋???宸ヤ?瀹?set 濮???='"

??????????? + ((TextBox)(GridView1.Rows[e.RowIndex].Cells[1].Controls[0])).Text.ToString().Trim() + "',瀹跺涵浣???='"

??????????? + ((TextBox)(GridView1.Rows[e.RowIndex].Cells[3].Controls[0])).Text.ToString().Trim() + "' where 韬?浠借???風??='"

??????????? + GridView1.DataKeys[e.RowIndex].Value.ToString() + "'";

??????? sqlcom=new SqlCommand(sqlstr,sqlcon);

??????? sqlcon.Open();

??????? sqlcom.ExecuteNonQuery();

??????? sqlcon.Close();

??????? GridView1.EditIndex = -1;

??????? bind();

??? }

??? protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)

??? {

??????? GridView1.EditIndex = -1;

??????? bind();

??? }

??? public void bind()

??? {

??????? string sqlstr = "select top 5 * from 椋???宸ヤ?瀹?;

??????? sqlcon = new SqlConnection(strCon);

??????? SqlDataAdapter myda = new SqlDataAdapter(sqlstr, sqlcon);

??????? DataSet myds = new DataSet();

??????? sqlcon.Open();

??????? myda.Fill(myds, "椋???宸ヤ?瀹?);

??????? GridView1.DataSource = myds;

??????? GridView1.DataKeyNames = new string[] { "韬?浠借???風??" };

??????? GridView1.DataBind();

??????? sqlcon.Close();

??????? GridView1.Columns[3].Visible = false;

??????? CheckBox1.Checked = false;

??? }

??? protected void CheckBox1_CheckedChanged(object sender, EventArgs e)

??? {

???????? GridView1.Columns[3].Visible=! GridView1.Columns[3].Visible;

???????? Response.Write("GridView1??绗?4???闆?ㄧ???劇ず?????舵????锛?"+GridView1.Columns[3].Visible.ToString());

??? }

}

???頒唬??锛?

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >

<head runat="server">

??? <title>GridView?劇ず?????? 娓?娓?????ttp://blog.csdn.net/21aspnet?</title>

</head>

<body style="font-size=12px">

??? <form id="form1" runat="server">

??? <div>

?????????????????? <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" 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"? >

??????????????????????? <FooterStyle BackColor="White" ForeColor="#000066" />

??????????????????????? <Columns>

??????????????????????????? <asp:BoundField DataField="韬?浠借???風??" HeaderText="缂??? ReadOnly="True" />

??????????????????????????? <asp:BoundField DataField="濮???" HeaderText="?ㄦ?峰???" />

??????????????????????????? <asp:BoundField DataField="???跨???" HeaderText="???跨???" SortExpression="???跨???" />

??????????????????????????? <asp:BoundField DataField="瀹跺涵浣???" HeaderText="瀹跺涵浣???"? />

??????????????????????????? <asp:CommandField HeaderText="???? ShowSelectButton="True" />

??????????????????????????? <asp:CommandField HeaderText="缂?杈?" ShowEditButton="True" />

??????????????????????????? <asp:CommandField HeaderText="???? 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>

??????? <asp:CheckBox ID="CheckBox1" runat="server" AutoPostBack="True" Font-Size="12px"

??????????? OnCheckedChanged="CheckBox1_CheckedChanged" Text="?劇ず????瀹跺涵浣???" /></div>

??? </form>

</body>

</html>

?13.GridView寮瑰?烘?伴〉??寮瑰?哄?跺??澶у?浣?缃??扮???o?

?????撅?

asp.net GridView用法介紹

?規?涓?锛?绠??????規?锛??扮???d??哄??澶у?

?<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" 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"? >

??????????????????????? <FooterStyle BackColor="White" ForeColor="#000066" />

??????????????????????? <Columns>

??????????????????????????? <asp:BoundField DataField="韬?浠借???風??" HeaderText="缂??? ReadOnly="True" />

??????????????????????????? <asp:BoundField DataField="???跨???" HeaderText="???跨???" SortExpression="???跨???" />

??????????????????????????? <asp:BoundField DataField="瀹跺涵浣???" HeaderText="瀹跺涵浣???"? />

??????????????????????????? <asp:HyperLinkField HeaderText="濮???" Text="濮???"DataNavigateUrlFields="濮???" DataNavigateUrlFormatString="Default6.aspx?GoodsID={0}" Target="mainframe" NavigateUrl="~/Default6.aspx" DataTextField="濮???" >

??????????????????? </asp:HyperLinkField>

??????????????????????????? <asp:CommandField HeaderText="???? ShowSelectButton="True" />

??????????????????????????? <asp:CommandField HeaderText="缂?杈?" ShowEditButton="True" />

??????????????????????????? <asp:CommandField HeaderText="???? 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>

DataNavigateUrlFields???炬?ョ??瀛?娈靛??锛?DataNavigateUrlFormatString??璺?寰???

?規?浜?锛?绮劇‘?у?跺脊?虹???eぇ灏?浣?缃?

<asp:HyperLinkColumn DataNavigateUrlField="EmployeeID" DataNavigateUrlFormatString="javascript:varwin=window.open('detail.aspx?ID={0}',null,'width=300,height=200');window.Close();"

???????DataTextField="LastName" HeaderText="LastName"></asp:HyperLinkColumn>

浣跨?ㄧ????缁???javascript??window.open?規?锛??充?window.open?????扮?涓???寰?澶?甯?瀛?锛???绔?涔???璁稿?????

寮瑰?虹???eぇ?? http://blog.csdn.net/21aspnet/archive/2004/10/25/150231.aspx?? ?沖??锛?

??14.GridView?哄??琛ㄥご(涓???avascript????SS!锛?寰?濂界??锛?

?????撅?

asp.net GridView用法介紹

浠g??锛?

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >

<head runat="server">

??? <title>GridView?哄??琛ㄥご 娓?娓?????ttp://blog.csdn.net/21aspnet?</title>

??????? <style>

.Freezing

?? {

???

?? position:relative ;

?? table-layout:fixed;

?? top:expression(this.offsetParent.scrollTop);??

?? z-index: 10;

?? }

.Freezing th{text-overflow:ellipsis;overflow:hidden;white-space: nowrap;padding:2px;}

</style>

</head>

<body style="font-size=12px">

??? <form id="form1" runat="server">

??? <div style="overflow-y: scroll; height: 200px;width:300px" id="dvBody">

?????????????????? <asp:GridView ID="GridView1" runat="server"??? AutoGenerateColumns="False" 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" OnRowCreated="GridView1_RowCreated"? >

??????????????????????? <FooterStyle BackColor="White" ForeColor="#000066" />

??????????????????????? <Columns>

??????????????????????????? <asp:BoundField DataField="韬?浠借???風??" HeaderText="缂??? ReadOnly="True" />

??????????????????????????? <asp:BoundField DataField="???跨???" HeaderText="???跨???" SortExpression="???跨???" />

??????????????????????????? <asp:BoundField DataField="瀹跺涵浣???" HeaderText="瀹跺涵浣???"? />

??????????????????????????? <asp:BoundField DataField="濮???" HeaderText="濮???"? />

???????????????????????????

??????????????????????? </Columns>

??????????????????????? <RowStyle ForeColor="#000066" />

??????????????????????? <SelectedRowStyle BackColor="#669999" Font-Bold="True" ForeColor="White" />

??????????????????????? <PagerStyle BackColor="White" ForeColor="#000066" HorizontalAlign="Left"? CssClass="ms-formlabel DataGridFixedHeader"/>

??????????????????????? <HeaderStyle BackColor="#006699" Font-Bold="True" ForeColor="White"CssClass="Freezing"/>

??????????????????? </asp:GridView>

??????? </div>

??? </form>

</body>

</html>

?ㄦ?锛?CSS璁懼?涓????峰?锛?HeaderStyle??CssClass="Freezing锛?濂?浣?GridView??Div璁劇疆楂?搴??藉害 <div style="overflow-y: scroll; height: 200px;width:200px" >

15.GridView??骞惰〃澶村???琛ㄥご????瀹?缇???锛?浠ュ??骞???3琛?涓句?锛?

?????撅?

asp.net GridView用法介紹

???頒唬??锛?

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;

using System.Drawing;

public partial class _Default : System.Web.UI.Page

{

??? SqlConnection sqlcon;

??? SqlCommand sqlcom;

??? string strCon = "Data Source=(local);Database=??椋?璐告??;Uid=sa;Pwd=sa";

??? protected void Page_Load(object sender, EventArgs e)

??? {

??????? if (!IsPostBack)

??????? {

??????????? bind();

???????????

??????? }

??? }

??? protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)

??? {

??????? GridView1.EditIndex = e.NewEditIndex;

??????? bind();

??? }

??? protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)

??? {

??????? sqlcon = new SqlConnection(strCon);

??????? string sqlstr = "update 椋???宸ヤ?瀹?set 濮???='"

??????????? + ((TextBox)(GridView1.Rows[e.RowIndex].Cells[1].Controls[0])).Text.ToString().Trim() + "',瀹跺涵浣???='"

??????????? + ((TextBox)(GridView1.Rows[e.RowIndex].Cells[3].Controls[0])).Text.ToString().Trim() + "' where 韬?浠借???風??='"

??????????? + GridView1.DataKeys[e.RowIndex].Value.ToString() + "'";

??????? sqlcom=new SqlCommand(sqlstr,sqlcon);

??????? sqlcon.Open();

??????? sqlcom.ExecuteNonQuery();

??????? sqlcon.Close();

??????? GridView1.EditIndex = -1;

??????? bind();

??? }

??? protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)

??? {

??????? GridView1.EditIndex = -1;

??????? bind();

??? }

??? public void bind()

??? {

??????? string sqlstr = "select top 10 * from 椋???宸ヤ?瀹?;

??????? sqlcon = new SqlConnection(strCon);

??????? SqlDataAdapter myda = new SqlDataAdapter(sqlstr, sqlcon);

??????? DataSet myds = new DataSet();

??????? sqlcon.Open();

??????? myda.Fill(myds, "椋???宸ヤ?瀹?);

??????? GridView1.DataSource = myds;

??????? GridView1.DataKeyNames = new string[] { "韬?浠借???風??" };

??????? GridView1.DataBind();

??????? sqlcon.Close();

??? }

//杩???灏辨??瑙e?蟲?規?

??? protected void GridView1_RowCreated(object sender, GridViewRowEventArgs e)

??? {

??????? switch (e.Row.RowType)

??????? {

??????????? case DataControlRowType.Header:

??????????????? //绗?涓?琛?琛ㄥご

??????????????? TableCellCollection tcHeader = e.Row.Cells;

??????????????? tcHeader.Clear();

??????????????? tcHeader.Add(new TableHeaderCell());

??????????????? tcHeader[0].Attributes.Add("rowspan", "3");?//璺?ow

??????????????? tcHeader[0].Attributes.Add("bgcolor", "white");

??????????????? tcHeader[0].Text = "";

??????????????? tcHeader.Add(new TableHeaderCell());

??????????????? //tcHeader[1].Attributes.Add("bgcolor", "Red");

??????????????? tcHeader[1].Attributes.Add("colspan", "6");?//璺?olumn

??????????????? tcHeader[1].Text = "?ㄩ?ㄤ俊??</th></tr><tr>";

??????????????? //绗?浜?琛?琛ㄥご

??????????????? tcHeader.Add(new TableHeaderCell());

??????????????? tcHeader[2].Attributes.Add("bgcolor", "DarkSeaGreen");

??????????????? tcHeader[2].Text = "韬?浠借???風??";

??????????????? tcHeader.Add(new TableHeaderCell());

??????????????? tcHeader[3].Attributes.Add("bgcolor", "LightSteelBlue");

??????????????? tcHeader[3].Attributes.Add("colspan", "2");

??????????????? tcHeader[3].Text = "?烘??淇℃??";

??????????????? tcHeader.Add(new TableHeaderCell());

??????????????? tcHeader[4].Attributes.Add("bgcolor", "DarkSeaGreen");

??????????????? tcHeader[4].Text = "绂???;

??????????????? tcHeader.Add(new TableHeaderCell());

??????????????? tcHeader[5].Attributes.Add("bgcolor", "LightSteelBlue");

??????????????? tcHeader[5].Attributes.Add("colspan", "2");

??????????????? tcHeader[5].Text = "??绯繪?瑰?</th></tr><tr>";

??????????????? //绗?涓?琛?琛ㄥご

??????????????? tcHeader.Add(new TableHeaderCell());

??????????????? tcHeader[6].Attributes.Add("bgcolor", "Khaki");

??????????????? tcHeader[6].Text = "韬?浠借???風??";

??????????????? tcHeader.Add(new TableHeaderCell());

??????????????? tcHeader[7].Attributes.Add("bgcolor", "Khaki");

??????????????? tcHeader[7].Text = "濮???";

??????????????? tcHeader.Add(new TableHeaderCell());

??????????????? tcHeader[8].Attributes.Add("bgcolor", "Khaki");

??????????????? tcHeader[8].Text = "?虹???ユ??";

??????????????? tcHeader.Add(new TableHeaderCell());

??????????????? tcHeader[9].Attributes.Add("bgcolor", "Khaki");

??????????????? tcHeader[9].Text = "??姘?;

??????????????? tcHeader.Add(new TableHeaderCell());

??????????????? tcHeader[10].Attributes.Add("bgcolor", "Khaki");

??????????????? tcHeader[10].Text = "瀹跺涵浣???";

??????????????? tcHeader.Add(new TableHeaderCell());

??????????????? tcHeader[11].Attributes.Add("bgcolor", "Khaki");

??????????????? tcHeader[11].Text = "???跨???";

??????????????? break;

??????? }

??? }

}

???幫?

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >

<head runat="server">

??? <title>GridView??骞跺???琛ㄥご琛ㄥご 娓?娓?????ttp://blog.csdn.net/21aspnet?</title>

</head>

<body >

??? <form id="form1" runat="server">

??? <div? >

?????????????????? <asp:GridView ID="GridView1" runat="server"??? AutoGenerateColumns="False" CellPadding="3"? OnRowEditing="GridView1_RowEditing"

??????????????????????? OnRowUpdating="GridView1_RowUpdating" OnRowCancelingEdit="GridView1_RowCancelingEdit" BackColor="White" BorderColor="#CCCCCC" BorderStyle="None" BorderWidth="1px" Font-Size="12px" OnRowCreated="GridView1_RowCreated"? >

??????????????????????? <FooterStyle BackColor="White" ForeColor="#000066" />

??????????????????????? <Columns>

??????????????????????????? <asp:CommandField HeaderText="缂?杈?" ShowEditButton="True" />

??????????????????????????? <asp:BoundField DataField="韬?浠借???風??" HeaderText="缂??? ReadOnly="True" />

??????????????????????????? <asp:BoundField DataField="濮???" HeaderText="濮???"? />

??????????????????????????? <asp:BoundField DataField="?虹???ユ??" HeaderText="???跨???"? />

???????????????????????????? <asp:BoundField DataField="璧瘋??" HeaderText="璧瘋??"? />

??????????????????????????? <asp:BoundField DataField="瀹跺涵浣???" HeaderText="瀹跺涵浣???"? />

??????????????????????????? <asp:BoundField DataField="???跨???" HeaderText="???跨???" />

??????????????????????????

??????????????????????? </Columns>

??????????????????????? <RowStyle ForeColor="#000066" />

??????????????????????? <SelectedRowStyle BackColor="#669999" Font-Bold="True" ForeColor="White" />

??????????????????????? <PagerStyle BackColor="White" ForeColor="#000066" HorizontalAlign="Left"? CssClass="ms-formlabel DataGridFixedHeader"/>

??????????????????????? <HeaderStyle BackColor="#006699" Font-Bold="True" ForeColor="White" />

??????????????????? </asp:GridView>

??????? </div>

??? </form>

</body>

</html>

16.GridView绐??烘?劇ず??涓??????鹼?渚?濡???棰?浣?浜?澶?灏?锛????頒????肩??锛?

?????撅?

asp.net GridView用法介紹

瑙e?蟲?規?锛?涓昏???缁?瀹???杩?婊? ?GridView1.DataBind();

??????? for (int i = 0; i <= GridView1.Rows.Count - 1; i++)

??????? {

??????????? DataRowView mydrv = myds.Tables["椋???宸ヤ?瀹?].DefaultView[i];

??????????? string score = Convert.ToString(mydrv["璧瘋??"]);

??????????? if (Convert.ToDouble(score) < 34297.00)//澶у?惰????規???蜂????佃?劇疆????oInt32绛?绛?

??????????? {

??????????????? GridView1.Rows[i].Cells[4].BackColor = System.Drawing.Color.Red;

??????????? }

??????? }

??????? sqlcon.Close();

?ㄩ?ㄥ???頒唬??锛?

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;

using System.Drawing;

public partial class Default7 : System.Web.UI.Page

{

??? SqlConnection sqlcon;

??? SqlCommand sqlcom;

??? string strCon = "Data Source=(local);Database=??椋?璐告??;Uid=sa;Pwd=sa";

??? protected void Page_Load(object sender, EventArgs e)

??? {

??????? if (!IsPostBack)

??????? {

??????????? bind();

??????? }

??? }

??? protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)

??? {

??????? GridView1.EditIndex = e.NewEditIndex;

??????? bind();

??? }

??? protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)

??? {

??????? sqlcon = new SqlConnection(strCon);

??????? string sqlstr = "update 椋???宸ヤ?瀹?set 濮???='"

??????????? + ((TextBox)(GridView1.Rows[e.RowIndex].Cells[1].Controls[0])).Text.ToString().Trim() + "',瀹跺涵浣???='"

??????????? + ((TextBox)(GridView1.Rows[e.RowIndex].Cells[3].Controls[0])).Text.ToString().Trim() + "' where 韬?浠借???風??='"

??????????? + GridView1.DataKeys[e.RowIndex].Value.ToString() + "'";

??????? sqlcom = new SqlCommand(sqlstr, sqlcon);

??????? sqlcon.Open();

??????? sqlcom.ExecuteNonQuery();

??????? sqlcon.Close();

??????? GridView1.EditIndex = -1;

??????? bind();

??? }

??? protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)

??? {

??????? GridView1.EditIndex = -1;

??????? bind();

??? }

??? public void bind()

??? {

??????? string sqlstr = "select top 10 * from 椋???宸ヤ?瀹?;

??????? sqlcon = new SqlConnection(strCon);

??????? SqlDataAdapter myda = new SqlDataAdapter(sqlstr, sqlcon);

??????? DataSet myds = new DataSet();

??????? sqlcon.Open();

??????? myda.Fill(myds, "椋???宸ヤ?瀹?);

??????? GridView1.DataSource = myds;

??????? GridView1.DataKeyNames = new string[] { "韬?浠借???風??" };

??????? GridView1.DataBind();

??????? for (int i = 0; i <= GridView1.Rows.Count - 1; i++)

??????? {

??????????? DataRowView mydrv = myds.Tables["椋???宸ヤ?瀹?].DefaultView[i];

??????????? string score = Convert.ToString(mydrv["璧瘋??"]);

??????????? if (Convert.ToDouble(score) < 34297.00)//澶у?惰????規???蜂????佃?劇疆????oInt32绛?绛?

??????????? {

??????????????? GridView1.Rows[i].Cells[4].BackColor = System.Drawing.Color.Red;

??????????? }

??????? }

??????? sqlcon.Close();

??? }

}

???頒唬??锛?

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >

<head id="Head1" runat="server">

??? <title>GridView绐??烘?劇ず??涓???????娓?娓?????ttp://blog.csdn.net/21aspnet?</title>

</head>

<body >

??? <form id="form1" runat="server">

??? <div? >

?????????????????? <asp:GridView ID="GridView1" runat="server"??? AutoGenerateColumns="False" CellPadding="3"? OnRowEditing="GridView1_RowEditing"

??????????????????????? OnRowUpdating="GridView1_RowUpdating" OnRowCancelingEdit="GridView1_RowCancelingEdit" BackColor="White" BorderColor="#CCCCCC" BorderStyle="None" BorderWidth="1px" Font-Size="12px"? >

??????????????????????? <FooterStyle BackColor="White" ForeColor="#000066" />

??????????????????????? <Columns>

??????????????????????????? <asp:CommandField HeaderText="缂?杈?" ShowEditButton="True" />

??????????????????????????? <asp:BoundField DataField="韬?浠借???風??" HeaderText="缂??? ReadOnly="True" />

??????????????????????????? <asp:BoundField DataField="濮???" HeaderText="濮???"? />

??????????????????????????? <asp:BoundField DataField="?虹???ユ??" HeaderText="???跨???"? />

???????????????????????????? <asp:BoundField DataField="璧瘋??" HeaderText="璧瘋??"? DataFormatString="{0:C}" HtmlEncode="false"/>

??????????????????????????? <asp:BoundField DataField="瀹跺涵浣???" HeaderText="瀹跺涵浣???"? />

??????????????????????????? <asp:BoundField DataField="???跨???" HeaderText="???跨???" />

??????????????????????????

??????????????????????? </Columns>

??????????????????????? <RowStyle ForeColor="#000066" />

??????????????????????? <SelectedRowStyle BackColor="#669999" Font-Bold="True" ForeColor="White" />

??????????????????????? <PagerStyle BackColor="White" ForeColor="#000066" HorizontalAlign="Left"? CssClass="ms-formlabel DataGridFixedHeader"/>

??????????????????????? <HeaderStyle BackColor="#006699" Font-Bold="True" ForeColor="White" />

??????????????????? </asp:GridView>

??????? </div>

??? </form>

</body>

</html>

?17.GridView???ヨ???ㄦ???姹?骞沖???煎?璁?

?????撅?

asp.net GridView用法介紹

瑙e?蟲?規?锛?????

private double sum = 0;//????瀹??????版????锛?浣?瑕??規???蜂????靛?瑰????戒?瑕?澶???????int

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)

??? {

???????

??????? if (e.Row.RowIndex >= 0)

??????? {

??????????? sum += Convert.ToDouble(e.Row.Cells[6].Text);

??????? }

??????? else if (e.Row.RowType == DataControlRowType.Footer)

??????? {

??????????? e.Row.Cells[5].Text = "?昏??姘翠負锛?";

??????????? e.Row.Cells[6].Text = sum.ToString();

??????????? e.Row.Cells[3].Text = "骞沖????姘翠負锛?";

??????????? e.Row.Cells[4].Text = ((int)(sum / GridView1.Rows.Count)).ToString();

???????????

??????? }

??? }

???闆?ㄩ?ㄤ唬??锛?

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;

using System.Drawing;

public partial class Default7 : System.Web.UI.Page

{

??? SqlConnection sqlcon;

??? SqlCommand sqlcom;

??? string strCon = "Data Source=(local);Database=??椋?璐告??;Uid=sa;Pwd=sa";

??? protected void Page_Load(object sender, EventArgs e)

??? {

??????? if (!IsPostBack)

??????? {

??????????? bind();

??????? }

??? }

??? protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)

??? {

??????? GridView1.EditIndex = e.NewEditIndex;

??????? bind();

??? }

??? protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)

??? {

??????? sqlcon = new SqlConnection(strCon);

??????? string sqlstr = "update 椋???宸ヤ?瀹?set 濮???='"

??????????? + ((TextBox)(GridView1.Rows[e.RowIndex].Cells[1].Controls[0])).Text.ToString().Trim() + "',瀹跺涵浣???='"

??????????? + ((TextBox)(GridView1.Rows[e.RowIndex].Cells[3].Controls[0])).Text.ToString().Trim() + "' where 韬?浠借???風??='"

??????????? + GridView1.DataKeys[e.RowIndex].Value.ToString() + "'";

??????? sqlcom = new SqlCommand(sqlstr, sqlcon);

??????? sqlcon.Open();

??????? sqlcom.ExecuteNonQuery();

??????? sqlcon.Close();

??????? GridView1.EditIndex = -1;

??????? bind();

??? }

??? protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)

??? {

??????? GridView1.EditIndex = -1;

??????? bind();

??? }

??? public void bind()

??? {

??????? string sqlstr = "select top 5 * from 椋???宸ヤ?瀹?;

??????? sqlcon = new SqlConnection(strCon);

??????? SqlDataAdapter myda = new SqlDataAdapter(sqlstr, sqlcon);

??????? DataSet myds = new DataSet();

??????? sqlcon.Open();

??????? myda.Fill(myds, "椋???宸ヤ?瀹?);

??????? GridView1.DataSource = myds;

??????? GridView1.DataKeyNames = new string[] { "韬?浠借???風??" };

??????? GridView1.DataBind();

??????? sqlcon.Close();

??? }

??? private double sum = 0;//????瀹??????版????

??? protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)

??? {

???????

??????? if (e.Row.RowIndex >= 0)

??????? {

??????????? sum += Convert.ToDouble(e.Row.Cells[6].Text);

??????? }

??????? else if (e.Row.RowType == DataControlRowType.Footer)

??????? {

??????????? e.Row.Cells[5].Text = "?昏??姘翠負锛?";

??????????? e.Row.Cells[6].Text = sum.ToString();

??????????? e.Row.Cells[3].Text = "骞沖????姘翠負锛?";

??????????? e.Row.Cells[4].Text = ((int)(sum / GridView1.Rows.Count)).ToString();

???????????

??????? }

??? }

}

???幫???涓????卞ご灏辨??璁劇疆ShowFooter="True" 锛?????榛?璁よ〃澶翠負??????锛?

<asp:GridView ID="GridView1" runat="server"??? AutoGenerateColumns="False" CellPadding="3"? OnRowEditing="GridView1_RowEditing"

??????????????????????? OnRowUpdating="GridView1_RowUpdating" OnRowCancelingEdit="GridView1_RowCancelingEdit" BackColor="White" BorderColor="#CCCCCC" BorderStyle="None" BorderWidth="1px" Font-Size="12px" OnRowDataBound="GridView1_RowDataBound" ShowFooter="True"? >

??????????????????????? <FooterStyle BackColor="White" ForeColor="#000066" />

??????????????????????? <Columns>

??????????????????????????? <asp:CommandField HeaderText="缂?杈?" ShowEditButton="True" />

??????????????????????????? <asp:BoundField DataField="韬?浠借???風??" HeaderText="缂??? ReadOnly="True" />

??????????????????????????? <asp:BoundField DataField="濮???" HeaderText="濮???"? />

??????????????????????????? <asp:BoundField DataField="?虹???ユ??" HeaderText="???跨???"? />

??????????????????????????? <asp:BoundField DataField="瀹跺涵浣???" HeaderText="瀹跺涵浣???"? />

??????????????????????????? <asp:BoundField DataField="???跨???" HeaderText="???跨???" />

??????????????????????????? <asp:BoundField DataField="璧瘋??" HeaderText="璧瘋??"? />

??????????????????????????

??????????????????????? </Columns>

??????????????????????? <RowStyle ForeColor="#000066" />

??????????????????????? <SelectedRowStyle BackColor="#669999" Font-Bold="True" ForeColor="White" />

??????????????????????? <PagerStyle BackColor="White" ForeColor="#000066" HorizontalAlign="Left"? CssClass="ms-formlabel DataGridFixedHeader"/>

??????????????????????? <HeaderStyle BackColor="#006699" Font-Bold="True" ForeColor="White" />

??????????????????? </asp:GridView>

?

?18.GridView?版??瀵煎??xcel/Excel?版??璇誨??ridView

?????撅?

asp.net GridView用法介紹

瑙e?蟲?規?锛?

椤甸?㈠???涓?涓?????锛????諱?浠舵坊??濡?涓??規?锛?

protected void Button1_Click(object sender, EventArgs e)

??? {

??????? Export("application/ms-excel", "瀛?????缁╂?ヨ〃.xls");

??? }

??? private void Export(string FileType, string FileName)

??? {

??????? Response.Charset = "GB2312";

??????? Response.ContentEncoding = System.Text.Encoding.UTF7;

??????? Response.AppendHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(FileName, Encoding.UTF8).ToString());

??????? Response.ContentType = FileType;

??????? this.EnableViewState = false;

??????? StringWriter tw = new StringWriter();

??????? HtmlTextWriter hw = new HtmlTextWriter(tw);

??????? GridView1.RenderControl(hw);

??????? Response.Write(tw.ToString());

??????? Response.End();

??? }

//濡???娌℃??涓??㈡?規?浼??ラ??绫誨????GridView?????т歡??GridView1??蹇?椤繪?懼?ㄥ?鋒?? runat=server ??绐?浣???璁闆??

??? public override void VerifyRenderingInServerForm(Control control)

??? {

??? }

杩????變?????浠舵??浣???浠ヨ?寮??ュ??绉扮┖??O??Text

???頒唬??锛?

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;

using System.Drawing;

using System.IO;

using System.Text;

public partial class Default7 : System.Web.UI.Page

{

??? SqlConnection sqlcon;

??? SqlCommand sqlcom;

??? string strCon = "Data Source=(local);Database=??椋?璐告??;Uid=sa;Pwd=sa";

??? protected void Page_Load(object sender, EventArgs e)

??? {

??????? if (!IsPostBack)

??????? {

??????????? bind();

??????? }

??? }

???

??? public void bind()

??? {

??????? string sqlstr = "select top 5 * from 椋???宸ヤ?瀹?;

??????? sqlcon = new SqlConnection(strCon);

??????? SqlDataAdapter myda = new SqlDataAdapter(sqlstr, sqlcon);

??????? DataSet myds = new DataSet();

??????? sqlcon.Open();

??????? myda.Fill(myds, "椋???宸ヤ?瀹?);

??????? GridView1.DataSource = myds;

??????? GridView1.DataKeyNames = new string[] { "韬?浠借???風??" };

??????? GridView1.DataBind();

??????? sqlcon.Close();

??? }

??? protected void Button1_Click(object sender, EventArgs e)

??? {

??????? Export("application/ms-excel", "瀛?????缁╂?ヨ〃.xls");

??? }

??? private void Export(string FileType, string FileName)

??? {

??????? Response.Charset = "GB2312";

??????? Response.ContentEncoding = System.Text.Encoding.UTF7;

??????? Response.AppendHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(FileName, Encoding.UTF8).ToString());

??????? Response.ContentType = FileType;

??????? this.EnableViewState = false;

??????? StringWriter tw = new StringWriter();

??????? HtmlTextWriter hw = new HtmlTextWriter(tw);

??????? GridView1.RenderControl(hw);

??????? Response.Write(tw.ToString());

??????? Response.End();

??? }

??? public override void VerifyRenderingInServerForm(Control control)

??? {

??? }

???

}

???幫?

<asp:GridView ID="GridView1" runat="server"??? AutoGenerateColumns="False" CellPadding="3"?

???????????????????????? BackColor="White" BorderColor="#CCCCCC" BorderStyle="None" BorderWidth="1px" Font-Size="12px"?? >

??????????????????????? <FooterStyle BackColor="White" ForeColor="#000066" />

??????????????????????? <Columns>

??????????????????????????? <asp:BoundField DataField="韬?浠借???風??" HeaderText="缂??? ReadOnly="True" />

??????????????????????????? <asp:BoundField DataField="濮???" HeaderText="濮???"? />

??????????????????????????? <asp:BoundField DataField="?虹???ユ??" HeaderText="???跨???"? />

??????????????????????????? <asp:BoundField DataField="瀹跺涵浣???" HeaderText="瀹跺涵浣???"? />

??????????????????????????? <asp:BoundField DataField="???跨???" HeaderText="???跨???" />

??????????????????????????? <asp:BoundField DataField="璧瘋??" HeaderText="璧瘋??"? />

??????????????????????????

??????????????????????? </Columns>

??????????????????????? <RowStyle ForeColor="#000066" />

??????????????????????? <SelectedRowStyle BackColor="#669999" Font-Bold="True" ForeColor="White" />

??????????????????????? <PagerStyle BackColor="White" ForeColor="#000066" HorizontalAlign="Left"? CssClass="ms-formlabel DataGridFixedHeader"/>

??????????????????????? <HeaderStyle BackColor="#006699" Font-Bold="True" ForeColor="White" />

??????????????????? </asp:GridView>

??????? <asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="瀵煎?? />

璇誨??Excel?版????浠g??锛?杩?涓?寰?绠?????

private DataSet CreateDataSource()

??? {

??????? string strCon;

??????? strCon = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + Server.MapPath("excel.xls") + "; Extended Properties=Excel 8.0;";

??????? OleDbConnection olecon = new OleDbConnection(strCon);

??????? OleDbDataAdapter myda = new OleDbDataAdapter("SELECT * FROM [Sheet1$]", strCon);

??????? DataSet myds = new DataSet();

??????? myda.Fill(myds);

??????? return myds;

??? }

??? protected void Button1_Click(object sender, EventArgs e)

??? {

??????? GridView1.DataSource = CreateDataSource();

??????? GridView1.DataBind();

??? }