您的位置:知识库 » .NET技术

ASP.NET 2.0数据教程之十一:基于数据的自定义格式化

作者: xwang  来源: 博客园  发布时间: 2008-10-14 13:52  阅读: 5266 次  推荐: 0   原文链接   [收藏]  

系列文章导航:

ASP.NET 2.0数据教程之一:创建一个数据访问层

ASP.NET 2.0数据教程之二:创建一个业务逻辑层

ASP.NET 2.0数据教程之三:母板页和站点导航

ASP.NET 2.0数据教程之四:使用ObjectDataSource展现数据

ASP.NET 2.0数据教程之五:声明参数

ASP.NET 2.0数据教程之六:编程设置ObjectDataSource的参数值

ASP.NET 2.0数据教程之七:使用DropDownList过滤的主/从报表

ASP.NET 2.0数据教程之八:使用两个DropDownList过滤的主/从报表

ASP.NET 2.0数据教程之九:跨页面的主/从报表

ASP.NET 2.0数据教程之十:使用 GridView 和DetailView实现的主/从报表

ASP.NET 2.0数据教程之十一:基于数据的自定义格式化

ASP.NET 2.0数据教程之十二:在GridView控件中使用TemplateField


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

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

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


10: 创建GridViewRowDataBound事件的事件处理

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

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 GridViewDataSource中的一条记录

·         EmptyDataRowGridViewDataSource显示出来的某一行为空

·         Footer 底部行; 显示由GridViewShowFooter属性决定

·         Header 头部行; 显示由GridViewShowHeader属性决定

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

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

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

 

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...

            }

        }

}

 

0
0

.NET技术热门文章

    .NET技术最新文章

      最新新闻

        热门新闻