GridView 格式化特定值

     如果希望格式化某个特定的行甚至某个单元格,解决方法是相应GridView。RowDataBind事件,该事件在网格的一部分(标题,题注,分页,普通行或交替数据行,选定项目)被创建时发生。GridViewRow.DataItem属性提供指定行的数据对象,而通过 GridViewRow.Cells集合可以读取行的内容,通过GridViewRow可以改变颜色和对齐方式,增加或删除子控件等。
    下面的示例处理RowDataBound时间并按如下的规则设置颜色
        1. 如果称呼是Ms或Mrs,则将背景设为粉色,前景设为栗色。
        2. 如果称呼是Mr,背景色设为深蓝色,前景色为淡青色。
        3. 对于其他通用称呼入Dr,则呈现通过GridView.BackColor属性设置背景颜色

    RowDataBound事件代码如下:
   
protected   void  gridEmployees_RowDataBound( object  sender, GridViewRowEventArgs e)
ExpandedBlockStart.gifContractedBlock.gif    
{
        
if (e.Row.RowType == DataControlRowType.DataRow)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
            
// Get the title of courtesy for the item that's being created.
            string title = (string)DataBinder.Eval(e.Row.DataItem, "TitleOfCourtesy");

            
// If the title of courtesy is "Ms.", "Mrs.", or "Mr.",
            
// change the item's colors.
            if (title == "Ms." || title == "Mrs.")
ExpandedSubBlockStart.gifContractedSubBlock.gif            
{
                e.Row.BackColor 
= System.Drawing.Color.LightPink;
                e.Row.ForeColor 
= System.Drawing.Color.Maroon;
            }

            
else if (title == "Mr.")
ExpandedSubBlockStart.gifContractedSubBlock.gif            
{
                e.Row.BackColor 
= System.Drawing.Color.LightCyan;
                e.Row.ForeColor 
= System.Drawing.Color.DarkBlue;
            }

        }


    }

  
                    

  这里使用DataBinder.Eval方法通过反射从数据中读取信息


转载于:https://www.cnblogs.com/zjz008/archive/2009/09/21/1571432.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值