135-1821-9792

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

如何在RowDataBound的事件处理中编码确定数据对应的值,针对这个问题,这篇文章详细介绍了相对应的分析和解答,希望可以帮助更多想解决这个问题的小伙伴找到更简单易行的方法。

创新互联长期为1000多家客户提供的网站建设服务,团队从业经验10年,关注不同地域、不同群体,并针对不同对象提供差异化的产品和服务;打造开放共赢平台,与合作伙伴共同营造健康的互联网生态环境。为盱眙企业提供专业的网站建设、网站制作盱眙网站改版等技术服务。拥有10年丰富建站经验和众多成功案例,为您定制开发。

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

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

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

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

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

protected void HighlightCheapProducts_RowDataBound(object sender, GridViewRowEventArgs e)   {   }

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

protected void HighlightCheapProducts_RowDataBound(object sender, GridViewRowEventArgs e)       {        // Get the ProductsRow object from the DataItem property...           Northwind.ProductsRow product = (Northwind.ProductsRow)((System.Data.DataRowView)e.Row.DataItem).Row;           if (!product.IsUnitPriceNull() && product.UnitPrice <  10m)           {               // TODO: Highlight the row yellow...          }       }

当运用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属性来确定:

protected void HighlightCheapProducts_RowDataBound(object sender, GridViewRowEventArgs e)   {           // Make sure we are working with a DataRow           if (e.Row.RowType == DataControlRowType.DataRow)           {               // Get the ProductsRow object from the DataItem property...               Northwind.ProductsRow product = (Northwind.ProductsRow)((System.Data.DataRowView)e.Row.DataItem).Row;               if (!product.IsUnitPriceNull() && product.UnitPrice <  10m)               {                   // TODO: Highlight row yellow...               }           }   }

关于如何在RowDataBound的事件处理中编码确定数据对应的值问题的解答就分享到这里了,希望以上内容可以对大家有一定的帮助,如果你还有很多疑惑没有解开,可以关注创新互联行业资讯频道了解更多相关知识。


分享名称:如何在RowDataBound的事件处理中编码确定数据对应的值
浏览路径:http://kswsj.com/article/jicdgs.html

其他资讯



Copyright © 2009-2022 www.kswsj.com 成都快上网科技有限公司 版权所有 蜀ICP备19037934号