如何从 VB 格式化 DataGrid 中的日期列

发布于 2024-10-29 05:12:05 字数 537 浏览 5 评论 0原文

我有一个包含数据网格的 .aspx 文件:

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

与其关联的 .aspx.vb 文件用数据网格对象填充它。

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

一切都很好。不过,我想用特定的日期格式来格式化其中一列。有没有办法在vb代码中做到这一点?我知道我可以通过指定 AutoGenerateColumns="False" 然后显式定义列来在 .aspx 中执行此操作,但我想在代码中执行此操作,因为它对我的应用程序来说更具未来性。

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

<asp:DataGrid ID="Gifts"
AutoGenerateColumns="True"
runat="server" AllowSorting="True">
    </asp: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()

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

好听的两个字的网名 2024-11-05 05:12:05

您可以在 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,它的工作原理几乎相同。使用数据网格
ItemDataBound - 事件。

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

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

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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文