ASP.NET 2.0中Gridview控件高级技巧图文教程

作者:廖煜嵘 来源:天极网 时间:2007-08-07 15:46:00 

   
在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的话,则设置其行的背景颜色为黄色。程序运行结果如下图所示:



标签:ASP.NET,Gridview,技巧,2.0
0
投稿

猜你喜欢

  • python能做哪方面的工作

    2023-02-01 09:48:44
  • Python3搜索及替换文件中文本的方法

    2023-08-24 04:33:44
  • python文件编译为pyc后运行的实现步骤

    2021-03-08 22:36:46
  • Python实现计算圆周率π的值到任意位的方法示例

    2021-09-08 16:47:09
  • Google logo “我的中国”谷歌国际少年绘画大赛小学1-3年级

    2008-12-19 12:26:00
  • Python标准模块--ContextManager上下文管理器的具体用法

    2022-03-02 00:22:24
  • python对html过滤处理的方法

    2023-04-25 15:28:57
  • python日期时间转为字符串或者格式化输出的实例

    2021-06-26 17:12:12
  • Python中的if、else、elif语句用法简明讲解

    2023-05-18 22:18:59
  • mysql5在rhel5下乱码问题及解决方法

    2010-12-03 16:26:00
  • 原生JS实现的简单轮播图功能【适合新手】

    2024-04-27 15:22:44
  • Python实现的多线程http压力测试代码

    2021-06-30 23:47:29
  • Python Prim算法通过遍历墙实现迷宫的生成

    2022-06-26 08:41:09
  • Python特效之文字成像方法详解

    2021-08-09 09:34:06
  • Python利用OpenCV和skimage实现图像边缘检测

    2023-01-27 01:08:43
  • 解决安装pycharm后不能执行python脚本的问题

    2023-07-25 06:51:04
  • 使用python生成云词图实现画红楼梦词云图

    2022-07-19 00:21:56
  • 巧妙的自关联运用

    2012-10-07 10:55:58
  • Python bsddb模块操作Berkeley DB数据库介绍

    2024-01-18 05:32:02
  • 详解LyScript 内存扫描与查壳实现

    2022-04-18 07:07:31
  • asp之家 网络编程 m.aspxhome.com