Gridview TemplateField 显示日期

本文介绍了在ASP.NET中使用GridView展示数据时,如何将日期字段格式化为yyyy-MM-dd形式的方法。包括直接在模板字段中使用Eval进行格式化、在代码后面通过OnRowDataBound事件设置日期格式等五种不同的实现方式。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

方法一:

代码
< asp:TemplateField  HeaderText ="检查日期" >            
            
< ItemTemplate >
                
<% Eval ( " InspectionDate " " {0:yyyy-MM-dd} " ) %>                
            
</ ItemTemplate >
        
</ asp:TemplateField >

 

方法二:

代码
< asp:TemplateField  HeaderText ="检查日期" >            
            
< ItemTemplate >
                
<% string .Format( " {0:yyyy-MM-dd} " Eval ( " InspectionDate " ) %>            
            
</ ItemTemplate >
        
</ asp:TemplateField >

 

方法三:

先在TemplateField中放一个Label控件

代码
< asp:TemplateField  HeaderText ="检查日期" >            
            
< ItemTemplate >                 
                
< asp:Label  ID ="LabelInspectionDate"  runat ="server"  Text ="" ></ asp:Label >
            
</ ItemTemplate >
        
</ asp:TemplateField >

 

然后在cs中写OnRowDataBound事件

代码
  protected   void  xxxxx_RowDataBound( object  sender, GridViewRowEventArgs e)
    {
        DataRowView drv 
=  (DataRowView)e.Row.DataItem;
        
        
if  (e.Row.RowType  ==  DataControlRowType.DataRow)
        {
            
if  (e.Row.FindControl( " LabelInspectionDate " !=   null )
            {
                Label labelInspectionDate 
=  (Label)e.Row.FindControl( " LabelInspectionDate " );
                labelInspectionDate.Text 
=   string .Format( " {0:yyyy-MM-dd} " ,drv[ " InspectionDate " ]);
            } 
        }
    }

 

方法五:
此方法和方法四有点相似,只是引用InsusDateTimeUtility自定义类别

代码
protected   void  xxxxx_RowDataBound( object  sender, GridViewRowEventArgs e)
    {
    InsusDateTimeUtility  objInsusDateTimeUtility 
=   new    InsusDateTimeUtility();    
     DataRowView drv 
=  (DataRowView)e.Row.DataItem;
        
        
if  (e.Row.RowType  ==  DataControlRowType.DataRow)
        {
            
if  (e.Row.FindControl( " LabelInspectionDate " !=   null )
            {
                Label labelInspectionDate 
=  (Label)e.Row.FindControl( " LabelInspectionDate " );
                labelInspectionDate.Text 
=  objInsusDateTimeUtility.GetDateTime(drv[ " InspectionDate " ],  " yyyy-MM-dd " );
            } 
        }
    }

 

 

 

转载于:https://www.cnblogs.com/insus/archive/2010/05/14/1735135.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值