天天看点

gridview 72绝技

璇存??锛???澶??轰?涓?绯诲??锛???璋?绮鹃??璁?#璇?瑷?瑕??广??杩?涓?绯诲??娌℃??????椤哄?锛?涓?杩?灏介?????扮簿?????戒?涓???澧????寸??锛???绯诲??????濮??哄???csdn??瀹?璋㈣阿?虫敞??

C#绮鹃??

绗???璁?GridView 72??缁???

浣???锛?娓?娓?????

涓婚〉锛?http://blog.csdn.net/21aspnet/?????????? ?堕?达?2007.3.24-26

涓轰???????锛?????宸ュ??????锛?绱???2澶?瀹挎病?¤?锛?杩?绡???绔?瀹??ㄦ????璐规??澶?澶???蹇?琛?锛?????姘歌?灞?浜?CSDN绀惧?虹??娓?娓????匡?璇蜂?瑕?璇??句慨?规????锛??村???ㄦ????瀹??存?э?甯????ㄥ????????冲?ㄦ????锛?

蹇???棰?瑙?锛?

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????椤垫??搴?锛?

?????撅?

gridview 72绝技

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

2.榛?璁?椤?0?★?濡???瑕?淇??规??椤垫?℃?帮?淇???ageSize?冲??锛???spx浠g??涓???PageSize="12"??

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

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

?????撅?

gridview 72绝技

???颁唬??锛?

浣???浠ヤ娇??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娆$????搴?锛??瑰?朵?涔?涓??峰??浠ャ??

gridview 72绝技
gridview 72绝技

???颁唬??锛?

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缁???锛?

?????撅?

gridview 72绝技

???颁唬??锛?

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缁???锛?

?????撅?

gridview 72绝技

???颁唬??锛?

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??涓?琛??舵?瑰??璇ヨ????????叉?规?涓?锛?

?????撅?

gridview 72绝技

??娉?锛?

????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??涓?琛??舵?瑰??璇ヨ????????叉?规?浜?锛?

?????撅?

gridview 72绝技

??娉?锛???涓??㈢??涓??峰氨??浠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瀹??板???ゆ?跺脊?虹‘璁ゅ?硅??妗?锛?

?????撅?

gridview 72绝技

瀹??版?规?锛?

????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瀹??拌???ㄧ??凤?

?????撅?

gridview 72绝技

瀹??版?规?锛?

????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瀹??拌??瀹?涔??堕?磋揣甯?绛?瀛?绗?覆?煎?锛?

?????撅?

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

gridview 72绝技

??-?煎?????

gridview 72绝技

瑙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?胯??垮??绗?覆锛?

?????撅?

gridview 72绝技

瑙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涓????㈣?涓?寮哄?舵?㈣?锛?

?????撅?

gridview 72绝技

棣???璁剧疆<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?虫?规?锛?

?????撅?

??-寮?濮?

gridview 72绝技

??-?瑰?绘?剧ず??CheckBox??

gridview 72绝技

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

?????撅?

gridview 72绝技

?规?涓?锛?绠??????规?锛??扮???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!锛?寰?濂界??锛?

?????撅?

gridview 72绝技

浠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琛?涓句?锛?

?????撅?

gridview 72绝技

???颁唬??锛?

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绐??烘?剧ず??涓??????硷?渚?濡???棰?浣?浜?澶?灏?锛????颁????肩??锛?

?????撅?

gridview 72绝技

瑙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???ヨ???ㄦ???姹?骞冲???煎?璁?

?????撅?

gridview 72绝技

瑙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

?????撅?

gridview 72绝技

瑙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();

??? }

?变??堕?村?崇郴锛?杩?涓???绔????版?ゃ?????堕?村????锛??跺??杩???寰?澶???宸э?涓?杩???瑙?寰?濡????借??姹?璐???搴?璇ュ??浠ヤ妇涓???涓???

Posted by marslei

杞??筹?http://blog.csdn.net/21aspnet/archive/2007/03/25/1540301.aspx

杞?杞戒?:https://www.cnblogs.com/Silverlight_Team/archive/2009/02/23/1396777.html

继续阅读