動態(tài)網(wǎng)頁教程_ASP.NET 2.0中Gridview控件高級技巧
本文關(guān)鍵詞:ASP.NET2.0,由筆耕文化傳播整理發(fā)布。
天極軟件專題專區(qū)精選 到天極軟件“讀編交流區(qū)”暢所欲言
QQ掛機(jī) 了解Web2.0
ASP.NET 2.0中,新增加的gridview控件的確十分強(qiáng)大,彌補(bǔ)了在asp.net 1.1中,使用datagrid控件時的不足之處。因?yàn)樵赼sp.net 1.1中,在使用datagrid時,很多情況下依然要編寫大量的代碼,十分不方便,而且有時需要很多技巧。而在asp.net 2.0中,很多情況下,使用gridview控件的話,甚至只需要拖拉控件,設(shè)置屬性就可以了,不需要編寫任何代碼。在《使用ASP.NET 2.0中的GridView控件》和《ASP.NET2.0中用Gridview控件操作數(shù)據(jù)》中,已經(jīng)對gridview控件做了一系列介紹,如果之前沒有了解過gridview的讀者,請先閱讀這兩篇文章。在本文中,將繼續(xù)深入介紹gridview的一些使用技巧。
一 格式化gridview
和asp.net 1.1一樣,gridview可以很方便地定制其樣式,比如css,顏色等。要定制gridview的格式,十分簡單,,只需要鼠標(biāo)右擊gridview,在彈出的菜單中選擇"AUTO FORMAT",則可以選擇gridview的樣式,內(nèi)置了許多樣式,如下圖:
如果你要對gridview中每一列自定義格式,則只需要點(diǎn)擊gridview右上角的"smart tag"智能標(biāo)記,在彈出的菜單中,選擇"edit columns",會彈出如下圖的窗體,這樣就可以對每列進(jìn)行詳細(xì)的設(shè)置了:
比如,如果要某一列設(shè)置為特殊格式,如要將unitprice設(shè)置為貨幣格式,可以在unitprice列的DataFormatString屬性中設(shè)置為{0:C},程序代碼如下:
<%@ Page Language="C#" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<script runat="server">
</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Untitled Page</title>
</head>
<body>
<form runat="server">
。糳iv>
。糰sp:SqlDataSource
Runat="server"
SelectCommand="SELECT [ProductID], [ProductName],
[QuantityPerUnit], [UnitPrice], [UnitsInStock] FROM
[Products]"
ConnectionString="<%$ ConnectionStrings:NWConnectionString %>"
DataSourceMode="DataReader">
</asp:SqlDataSource>
。糰sp:GridView Runat="server"
DataSourceID="productsDataSource"
DataKeyNames="ProductID" AutoGenerateColumns="False"
BorderWidth="1px" BackColor="#DEBA84"
CellPadding="3" CellSpacing="2" BorderStyle="None"
BorderColor="#DEBA84">
<FooterStyle ForeColor="#8C4510" BackColor="#F7DFB5"></FooterStyle>
。糚agerStyle ForeColor="#8C4510" HorizontalAlign="Center"></PagerStyle>
。糎eaderStyle ForeColor="White" Font-Bold="True" BackColor="#A55129"></HeaderStyle>
<Columns>
。糰sp:BoundField ReadOnly="True" HeaderText="ID" InsertVisible="False" DataField="ProductID"
SortExpression="ProductID">
。糏temStyle HorizontalAlign="Center"></ItemStyle>
。/asp:BoundField>
<asp:BoundField HeaderText="Name" DataField="ProductName" SortExpression="ProductName">
。/asp:BoundField>
。糰sp:BoundField HeaderText="Qty/Unit"
DataField="QuantityPerUnit"
SortExpression="QuantityPerUnit"></asp:BoundField>
<asp:BoundField HeaderText="Price/Unit"
DataField="UnitPrice" SortExpression="UnitPrice"
DataFormatString="{0:c}">
。糏temStyle HorizontalAlign="Right"></ItemStyle>
。/asp:BoundField>
<asp:BoundField HeaderText="Units In Stock" DataField="UnitsInStock"
SortExpression="UnitsInStock"
DataFormatString="{0:d}">
。糏temStyle HorizontalAlign="Right"></ItemStyle>
。/asp:BoundField>
</Columns>
<SelectedRowStyle ForeColor="White" Font-Bold="True"
BackColor="#738A9C"></SelectedRowStyle>
<RowStyle ForeColor="#8C4510" BackColor="#FFF7E7"></RowStyle>
。/asp:GridView>
。/div>
。/form>
</body>
</html>
程序運(yùn)行后結(jié)果如下:
而有的時候,我們可能要根據(jù)需要,對gridview中的數(shù)據(jù)進(jìn)行特殊的顯示,比如當(dāng)某樣商品庫存為0時,要求gridview中以不同顏色進(jìn)行顯示,這時,可以按如下的方法進(jìn)行:
首先,gridview提供了rowdatabound事件,該事件在gridview中每行被創(chuàng)建并且綁定到datasource控件后被觸發(fā),因此,我們可以利用該事件去檢查庫存是否為0,如果為0的話,將所在行的北京顏色設(shè)置為黃色,代碼如下:
public void productsGridView_RowDataBound(object sender,
GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
int unitsInStock = Convert.ToInt32(DataBinder.Eval(e.Row.DataItem, "UnitsInStock"));
if (unitsInStock == 0)
e.Row.BackColor = Color.Yellow;
}
}
首先,該事件首先檢查,當(dāng)前的行是否屬于datarow類型的行,因?yàn)橄骻ridview中的headerrow,footerrow等行,并不包含實(shí)際的數(shù)據(jù),因此,我們不需要使用headerrow和footerrow,而為了取得庫存unitesinstock的內(nèi)容,通過使用databinder.eval的方法取出其內(nèi)容,并轉(zhuǎn)換為int類型,接著判斷是否為0,如果為0的話,則設(shè)置其行的背景顏色為黃色。程序運(yùn)行結(jié)果如下圖所示:
(作者:廖煜嶸責(zé)任編輯:方舟)
歡迎在新浪微博上關(guān)注我們
評論
* 網(wǎng)友發(fā)言均非本站立場,本站不在評論欄推薦任何網(wǎng)店、經(jīng)銷商,謹(jǐn)防上當(dāng)受騙!
本文關(guān)鍵詞:ASP.NET2.0,由筆耕文化傳播整理發(fā)布。
本文編號:196534
本文鏈接:http://sikaile.net/wenshubaike/xxkj/196534.html