如何从VB DataGrid中格式化日期列 [英] How to format a date column in a DataGrid from VB

查看:353
本文介绍了如何从VB DataGrid中格式化日期列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在它与数据网格中的.aspx文件:

I have a .aspx file with a datagrid in it:

<asp:DataGrid ID="Gifts"
AutoGenerateColumns="True"
runat="server" AllowSorting="True">
    </asp:DataGrid>

与它相关的.aspx.vb文件与DataGrid对象填充进去。

The .aspx.vb file associated with it fills it in with a datagrid object.

Protected WithEvents Gifts As System.Web.UI.WebControls.DataGrid
Public GiftData As DataTable
Gifts.DataSource = New DataView(GiftData)
Gifts.DataBind()

这一切工作正常。不过,我想格式化一个特定的日期格式的一列。是否有这样做,在VB code的一种方式?我知道我可以通过specifing的AutoGenerateColumns =FALSE,然后明确定义的列做在.aspx,但我想这样做在code,因为它是多项未来我的应用程序的证明。

That all works fine. However I want to format one of the columns with a particular date format. Is there a way of doing that in the vb code? I know I can do it in the .aspx by specifing AutoGenerateColumns="False" and then explicitly defining the columns, but I want to do it in the code as it's more future proof for my application.

推荐答案

您可以在的RowDataBound -Eventhandler:

You could do this in RowDataBound-Eventhandler:

Protected Sub GridView1_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles GridView1.RowDataBound
     If e.Row.RowType = DataControlRowType.DataRow Then
        'If the first column is a date
        e.Row.Cells(0).Text = String.Format("{0:D}", Date.Parse(e.Row.Cells(0).Text))
     End If
End Sub

如果你确实使用的是旧的DataGrid,它的工作原理几乎相同。使用DataGrid的
的ItemDataBound - 活动来代替。

If you're really using an old DataGrid, it works nearly the same. Use DataGrid's ItemDataBound-Event instead.

Protected Sub DataGrid1_ItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.DataGridItemEventArgs) Handles DataGrid1.ItemDataBound
     If e.Item.ItemType = ListItemType.Item orelse e.Item.ItemType = ListItemType.AlternatingItem Then
        'If the first column is a date
        e.Item.Cells(0).Text = String.Format("{0:D}", Date.Parse(e.Item.Cells(0).Text))
     End If
End Sub

这篇关于如何从VB DataGrid中格式化日期列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆