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

news/2024/7/10 20:53:12 标签: asp, server, object, xhtml, html, header
htmledit_views"> 我看到 论坛中有询问关于如何在GridView随某行某列值的改变时(这些值是空的或不是空的或是其它某些值等),其背景色及文本颜色也随之改变。这篇文章便论述这个问题。

   根据某列的值改变其样式最好的方法是在GridView的DataRowBound事件中想办法。在GridView中的行绑定数据后将立即执行 DataRowBound事件。DataRowBound事件使用GridViewRowEventargs类作为事件变量。通过事件变量你能够利用 GridViewRowEventArgs属性操作已经绑定数据的行。

protected void GridView1_RowDataBound(html" title=object>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(html" title=object>object sender, GridViewRowEventArgs e)
{
 if (e.Row.RowType == DataControlRowType.DataRow)
 {
  //Do something!
 }
}

   可以使用Row的Cells属性得到其Cells,它将返回一个TableCellCollection对象。然后通过 TableCellCollection索引得到特定的Cells。TableCellcollection索引将返回一个TabelCell对象,对应 于Row中的一个Cell:

protected void GridView1_RowDataBound(html" title=object>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/html" title=xhtml>xhtml11/DTD/html" title=xhtml>xhtml11.dtd">

<script runat="html" title=server>server">
protected void GridView1_RowDataBound(html" title=object>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/html" title=xhtml>xhtml" >
<head runat="html" title=server>server">
<title>Untitled Page</title>
</head>
<body>
 <form id="form1" runat="html" title=server>server">
 <div>
  <html" title=asp>asp:GridView ID="GridView1" runat="html" title=server>server" DataSourceID="SqlDataSource1" AutoGenerateColumns="False"
DataKeyNames="ProductID" OnRowDataBound="GridView1_RowDataBound">
  <Columns>
   <html" title=asp>asp:BoundField ReadOnly="True" HeaderText="ProductID" InsertVisible="False" DataField="ProductID"
SortExpression="ProductID" />
   <html" title=asp>asp:BoundField HeaderText="ProductName" DataField="ProductName" SortExpression="ProductName" />
   <html" title=asp>asp:BoundField HeaderText="QuantityPerUnit" DataField="QuantityPerUnit" SortExpression="QuantityPerUnit" />
   <html" title=asp>asp:BoundField HeaderText="UnitPrice" DataField="UnitPrice" SortExpression="UnitPrice" />
  </Columns>
 </html" title=asp>asp:GridView>
html" title=asp>asp:SqlDataSource ID="SqlDataSource1" runat="html" title=server>server" SelectCommand="SELECT [ProductID], [ProductName], [QuantityPerUnit], [UnitPrice] FROM [Alphabetical list of products]"

ConnectionString="<%$ ConnectionStrings:AppConnectionString1 %>" />

</div>
</form>
</body>
</html
 

http://www.niftyadmin.cn/n/1412917.html

相关文章

深入浅出Spring Security(三):FilterChainProxy的运行过程

上篇回顾 我们已经知道了Spring Security的核心过滤器的创建和原理,本文主要介绍核心过滤器FilterChainProxy是如何在tomcat的ServletContext中生效的。 ServletContext如何拿到FilterChainProxy的过滤器对象 我们都知道,Bean都是存在Spring的Bean工厂…

python遍历文件

很简单一个小脚本使用os模块和递归,遍历目录树,打出所有文件import osdef recursion(path):all_fileos.listdir(path)for file in all_file:file_pathos.path.join(path,file)if os.path.isdir(file_path):recursion(file_path)else:print file_pathrecu…

去掉Gridview显示的边框

<asp:GridView ID"GWClassList" runat"server" AutoGenerateColumns"False" DataKeyNames"NewsId"BorderStyle"None" CellPadding"0" GridLines "None">

小程序请求豆瓣API报403解决方法

微信小程序使用wx.request API请求豆瓣公开api的时候&#xff0c;会报一个403(Forbidden)的错误。这是为什么呢&#xff1f;是由于来自小程序的调用过多&#xff0c;豆瓣来自于小程序的调用被禁止。这里收集以下三种方法解决此问题&#xff08;设置代理&#xff09;&#xff1a…

深入浅出Spring Security(四):WebSecurity与HttpSecurity

上篇回顾 前面我们已经分析了Spring Security的核心过滤器FilterChainProxy的创建和运行过程&#xff0c;认识了建造者和配置器的作用。 现在我们知道WebSecurity作为一个建造者就是用来创建核心过滤器FilterChainProxy实例的。 WebSecurity在初始化的时候会扫描WebSecurity…

netty对http协议解析原理

本文主要介绍netty对http协议解析原理&#xff0c;着重讲解keep-alive&#xff0c;gzip&#xff0c;truncked等机制&#xff0c;详细描述了netty如何实现对http解析的高性能。 1 http协议 1.1 描述 标示 ASCII 描述 字符 CR 13 Carriage return (回车) \n LF 10 Line feed…

让GridView不显示表头

gridview1.ShowHeaderfalse

深入浅出Spring Security(五):认证和授权的过程

上篇回顾 上篇介绍了HttpSecurity如何建造过滤器链&#xff0c;本文主要介绍几个主要的过滤器。 认证过滤器 UsernamePasswordAuthenticationFilter 参数有username,password的&#xff0c;走UsernamePasswordAuthenticationFilter&#xff0c;提取参数构造UsernamePassword…