网管联盟 | 网管论坛 | 网管u家 | 网管博客 | 网管软件 | 网管求职 | 小游戏 | 网管搜索 | 网管原创 | 网管聚合 | 网管读摘 | 网管焦点 | 世界素材 | 会员投稿 | 会员中心 
中国网管联盟
Windows Linux Cisco 网络技术 数据库 黑客攻防 DotNet Java PHP 认证 新闻资讯 服务器 存储资讯 网络设备 网管学堂 技术专题 焦点 网吧频道
 当前位置: > bitsCN.com > JAVA > 核心技术 > 高级编程 > GridView根据值的变化改变行列样式  

GridView根据值的变化改变行列样式

2005-03-18  作者:BitsCN整理  来源:中国网管联盟  点评 投稿 收藏


  根据某列的值改变其样式最好的方法是在GridView的DataRowBound事件中想办法。在GridView中的行绑定数据后将立即执行DataRowBound事件。DataRowBound事件使用GridViewRowEventargs类作为事件变量。通过事件变量你能够利用GridViewRowEventArgs属性操作已经绑定数据的行。
  
  protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
  {
  GridViewRow row = e.Row;
  }
  
  Row将返回TableRow类中的一个GridViewRow对象。
  
  绑定的Row有几种不同的类型。例如:DataRow, EmptyDataRow, Footer, Header, Pager 和 Separator。通过GridView的RowType属性可以得到当前行的行类型。RowType是一组DataControlRow枚举。
  
  看下面的代码示例,检测GridView列出的行是否为一个标准类型的行。
  
  protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
  {
  if (e.Row.RowType == DataControlRowType.DataRow)
  {
  //Do something!
  }
  }
  
  可以使用Row的Cells属性得到其Cells,它将返回一个TableCellCollection对象。然后通过TableCellCollection索引得到特定的Cells。TableCellcollection索引将返回一个TabelCell对象,对应于Row中的一个Cell:
网管u家u.bitscn@com

  
  protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
  {
  if (e.Row.RowType == DataControlRowType.DataRow)
  {
  string value = e.Row.Cells[0].Text;
  }
  }
  
  现在你已经明白了如何得到GridView中某行某列的值,那么根据值的变化改变其样式就比较容易了。以下示例使用 Northwind 数据库,通过检测第四列(UnitPrice)的值是否大于10将其颜色改变为红色。
  
  <%@ Page Language="C#"%>
  <%@ Import Namespace="System.Drawing" %>
  
  <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
  
  <script runat="server">
  protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
  {
  if (e.Row.RowType == DataControlRowType.DataRow)
  {
  if (Decimal.Parse(e.Row.Cells[3].Text) > 10)
  e.Row.Cells[3].BackColor = Color.Red;
  }
  }
  
  </script>
  <html xmlns="http://www.w3.org/1999/xhtml" >
网管联盟bitsCN_com

  <head runat="server">
  <title>Untitled Page</title>
  </head>
  <body>
  <form id="form1" runat="server">
  <div>
  <asp:GridView ID="GridView1" runat="server" DataSourceID="SqlDataSource1" AutoGenerateColumns="False"
  DataKeyNames="ProductID" OnRowDataBound="GridView1_RowDataBound">
  <Columns>
  <asp:BoundField ReadOnly="True" HeaderText="ProductID" InsertVisible="False" DataField="ProductID"
  SortExpression="ProductID" />
  <asp:BoundField HeaderText="ProductName" DataField="ProductName" SortExpression="ProductName" />
  <asp:BoundField HeaderText="QuantityPerUnit" DataField="QuantityPerUnit" SortExpression="QuantityPerUnit" />
  <asp:BoundField HeaderText="UnitPrice" DataField="UnitPrice" SortExpression="UnitPrice" />
  </Columns>
  </asp:GridView>
  <asp:SqlDataSource ID="SqlDataSource1" runat="server" SelectCommand="SELECT [ProductID], [ProductName], [QuantityPerUnit], [UnitPrice] FROM [Alphabetical list of products]" 中国网管联盟bitsCN.com
  
  ConnectionString="<%$ ConnectionStrings:AppConnectionString1 %>" />
  
  </div>
  </form>
  </body>
  </html>

TAGs行列   样式   改变   变化   根据   事件   ProductID   server   UnitPrice    
 上一篇:在蹉跎中一路前行:谈微软.NET技术   下一篇:激情后的思考:.NET想说爱你不容易
相关文章列表
GridView根据值的变化改变行列样式 评论:
loading.. 评论加载中…
评论:请自觉遵守互联网相关政策法规,评论不得超过250字。

验证码: 注册用户
本类热门排行:
1.用java编写的小游戏源代码分析
2.在.Net程序中使用log4net记录日志
3.Java与sqlserver2000的连接
4.Java源代码案例--骑士巡游问题
5.Java游戏编程分析--棋子的拖动
6.JAVA 正则表达式4种常用的功能
7.Log4j的使用总结!(用于收藏救急用)
8.JFreeChart中柱状图的详细设置
9.学习教程:Java Annotation 高
10.JAVA对象转为Java String的几种常用方法
最新推荐文章:
1.该如何实践State模式
2.JFreeChart中柱状图的详细设置
3.Java Swing APIs可插拔式外观风格特性应
4.用信号量Semaphore实现互斥锁Mutex
5.使用Weblogic Integration的应用程序架构
6.设计Java应用程序
7.对Java嵌套类的讨论(上)
8.提高Java代码可重用性的三个措施
9.JAVA上加密算法的实现用例
10.总结篇:将内部类进行到底
网管论坛交流:
·不疯魔不成活
·令你大开眼界的真正标准化机房,已整理重
·为赈灾,女孩舍身拍“裸照”
·Windows Server 2003服务器群集创建和配
·exchange2k3全套官方资料
·双儿一周岁了。。。特殊的礼物来啦。。
·存储备份技术版块守则
·无盘技术交流区守则
·DOS命令基础大全之命令详解<作者吐血
·Windows XP 操作系统默认设置需要注意的