asp.net 2.0中,新增加的gridview控件的确十分强大,弥补了在asp.net 1.1中,使用datagrid控件时的不足之处。因为在asp.net 1.1中,在使用datagrid时,很多情况下依然要编写大量的代码,十分不方便,而且有时需要很多技巧。
而在asp.net 2.0中,很多情况下,使用gridview控件的话,甚至只需要拖拉控件,设置属性就可以了,不需要编写任何代码。在《使用asp.net 2.0中的gridview控件》和《asp.net2.0中用gridview控件操作数据》中,已经对gridview控件做了一系列介绍,如果之前没有了解过gridview的读者,请先阅读这两篇文章。在本文中,将继续深入介绍gridview的一些使用技巧。
一 格式化gridview
和asp.net 1.1一样,gridview可以很方便地定制其样式,比如css,颜色等。要定制gridview的格式,十分简单,只需要鼠标右击gridview,在弹出的菜单中选择"auto format",则可以选择gridview的样式,内置了许多样式,如下图:

如果你要对gridview中每一列自定义格式,则只需要点击gridview右上角的"smart tag"智能标记,在弹出的菜单中,选择"edit columns",会弹出如下图的窗体,这样就可以对每列进行详细的设置了:
比如,如果要某一列设置为特殊格式,如要将unitprice设置为货币格式,可以在unitprice列的dataformatstring属性中设置为{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 id="form1" runat="server">
<div>
<asp:sqldatasource id="productsdatasource"
runat="server"
selectcommand="select [productid], [productname],
[quantityperunit], [unitprice], [unitsinstock] from
[products]"
connectionstring="<%$ connectionstrings:nwconnectionstring %>"
datasourcemode="datareader">
</asp:sqldatasource>
<asp:gridview id="productgridview" 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>
<pagerstyle forecolor="#8c4510" horizontalalign="center"></pagerstyle>
<headerstyle forecolor="white" font-bold="true" backcolor="#a55129"></headerstyle>
<columns>
<asp:boundfield readonly="true" headertext="id" insertvisible="false" datafield="productid"
sortexpression="productid">
<itemstyle horizontalalign="center"></itemstyle>
</asp:boundfield>
<asp:boundfield headertext="name" datafield="productname" sortexpression="productname">
</asp:boundfield>
<asp:boundfield headertext="qty/unit"
datafield="quantityperunit"
sortexpression="quantityperunit"></asp:boundfield>
<asp:boundfield headertext="price/unit"
datafield="unitprice" sortexpression="unitprice"
dataformatstring="{0:c}">
<itemstyle horizontalalign="right"></itemstyle>
</asp:boundfield>
<asp:boundfield headertext="units in stock" datafield="unitsinstock"
sortexpression="unitsinstock"
dataformatstring="{0:d}">
<itemstyle 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>
程序运行后结果如下:
而有的时候,我们可能要根据需要,对gridview中的数据进行特殊的显示,比如当某样商品库存为0时,要求gridview中以不同颜色进行显示,这时,可以按如下的方法进行:
首先,gridview提供了rowdatabound事件,该事件在gridview中每行被创建并且绑定到datasource控件后被触发,因此,我们可以利用该事件去检查库存是否为0,如果为0的话,将所在行的北京颜色设置为黄色,代码如下:
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;
}
}
首先,该事件首先检查,当前的行是否属于datarow类型的行,因为象gridview中的headerrow,footerrow等行,并不包含实际的数据,因此,我们不需要使用headerrow和footerrow,而为了取得库存unitesinstock的内容,通过使用databinder.eval的方法取出其内容,并转换为int类型,接着判断是否为0,如果为0的话,则设置其行的背景颜色为黄色。程序运行结果如下图所示:
