在RowDataBound的事件处理中编码确定数据对应的值

开发 后端
本文简单介绍了如何在RowDataBound的事件处理中编码确定数据对应的值。

当ProductsDataTable绑定到GridView,GridView将会产生若干个ProductsRow。GridViewRow的DataItem属性将会生成一个实际的ProductRow。在GridView的 RowDataBound事件发生之后,为了确定UnitsInStock的值,我们需要创建RowDataBound的事件处理,在其中我们可以确定UnitsInStock的值并做相应的格式化

EventHandler的创建过程和前面两个一样

创建GridView的RowDataBound事件的事件处理 

RowDataBound: 创建GridView的RowDataBound事件的事件处理

在后台代码里将会自动生成如下代码

  1. protected void HighlightCheapProducts_RowDataBound(object sender, GridViewRowEventArgs e)  
  2.  
  3. {  
  4.  
  5. }  
  6.  

当RowDataBound事件触发,第二个参数GridViewRowEventArgs中包含了对GridViewRow的引用,我们用如下的代码来访问GridViewRow中的ProductsRow   

  1. protected void HighlightCheapProducts_RowDataBound(object sender, GridViewRowEventArgs e)  
  2.  
  3.     {        // Get the ProductsRow object from the DataItem property...  
  4.  
  5.         Northwind.ProductsRow product = (Northwind.ProductsRow)((System.Data.DataRowView)e.Row.DataItem).Row;  
  6.  
  7.         if (!product.IsUnitPriceNull() && product.UnitPrice <  10m)  
  8.  
  9.         {  
  10.  
  11.             // TODO: Highlight the row yellow...  
  12.  
  13.        }  
  14.  
  15.     }  
  16.  

当运用RowDataBound事件处理时,GridView由各种类型不同的行组成,而事件发生针对所有的行类型, GridViewRow的类型可以由RowType属性决定,可以是以下类型中的一种

·DataRow – GridView的DataSource中的一条记录

·EmptyDataRow – GridView的DataSource显示出来的某一行为空

·Footer – 底部行; 显示由GridView的ShowFooter属性决定

·Header – 头部行; 显示由GridView的ShowHeader属性决定

·Pager – GridView的分页,这一行显示分页的标记

·Separator – 对于GridView不可用,但是对于DataList和Reapter的RowType属性却很有用,我们将在将来的文章中讨论他们

当上面四种(DataRow, Pager Rows Footer, Header)都不合适对应值时,将返回一个空的数据项, 所以我们需要在代码中检查GridViewRow的RowType属性来确定:

  1. protected void HighlightCheapProducts_RowDataBound(object sender, GridViewRowEventArgs e)  
  2.  
  3. {  
  4.  
  5.         // Make sure we are working with a DataRow  
  6.  
  7.         if (e.Row.RowType == DataControlRowType.DataRow)  
  8.  
  9.         {  
  10.  
  11.             // Get the ProductsRow object from the DataItem property...  
  12.  
  13.             Northwind.ProductsRow product = (Northwind.ProductsRow)((System.Data.DataRowView)e.Row.DataItem).Row;  
  14.  
  15.             if (!product.IsUnitPriceNull() && product.UnitPrice <  10m)  
  16.  
  17.             {  
  18.  
  19.                 // TODO: Highlight row yellow...  
  20.  
  21.             }  
  22.  
  23.         }  
  24.  
  25. }  
  26.  

【编辑推荐】

  1. ASP.NET 2.0数据教程:SelectMethod属性的使用
  2. ASP.NET 2.0数据教程:在业务逻辑层添加方法
  3. ASP.NET 2.0数据教程:为TableAdapter添加方法
  4. ASP.NET 2.0数据教程:使用一个硬编码参数值
  5. ASP.NET 2.0数据教程:绑定到ObjectDataSource
责任编辑:book05 来源: 博客堂
相关推荐

2009-07-27 16:56:05

DataBound

2009-07-27 16:42:16

DataBound

2016-01-22 11:05:07

2010-03-23 14:34:44

Python vim检

2009-06-14 17:53:25

ibmdwWebSphere

2015-07-20 11:12:43

数据中心数据中心建设

2013-05-15 15:30:02

数据中心综合布线

2023-12-07 19:00:25

数据科学机器学习数据可视化

2017-03-14 13:51:23

AndroidView事件分发和处理

2017-12-27 14:22:07

数据中心负载成本

2016-04-29 10:02:39

2023-10-04 00:01:00

sizeofC 语言

2009-07-28 08:24:16

GridView绑定数

2011-06-16 14:23:43

JavaScript空事件处理程序

2021-02-06 10:27:45

C#函数参数

2011-04-01 14:14:42

SQL Server空值

2010-04-28 18:25:51

Oracle数据库

2010-05-31 15:23:02

MySQL数据库NUL

2021-09-02 13:49:37

复杂事件处理CEP数据安全

2020-09-25 11:10:51

运维故障排查监控
点赞
收藏

51CTO技术栈公众号