asp.net 在 gridview 中格式化日期时间 [英] asp.net formatting dateTime in gridview

查看:33
本文介绍了asp.net 在 gridview 中格式化日期时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在从我称为会话"的数据库中的表动态绑定 gridview.我从使用 linq 查询的方法中获取信息,如下所示:

I'm binding a gridview dynamically from a table in my database I have called "Sessions". I get the information from a method using a linq query that is something like this:

var s = from sessions in datacontext.Sessions
                    where sessions.test == id
                    orderby sessions.ID ascending
                    select sessions;
gridView.DataSource = qsessions;
gridView.DataBind();

Sessions 包含一个 dateTime 字段,我想将其最小化以仅显示日期(月/日/年).从我通过谷歌搜索阅读的内容来看,解决方案是使用以下内容在 gridview 的 aspx 标记中指定格式:

Sessions contains a dateTime field that I want to minimize to just display the date (month/day/year). From what I've read through google searches, the solution is to specify the formatting in the aspx markup of the gridview using something like:

<asp:TextBox ID="txtDate" runat="server" Text='<%# Eval("dateTime", "{0:MM/dd/yyyy}") %>'></asp:TextBox>

它似乎不起作用,仍然显示日期后的时间.有什么我想念的吗?非常感谢帮助!

It doesn't seem to work and still shows the time after the date. Is there something I'm missing? Help much appreciated!

推荐答案

尝试:

<asp:TextBox ID="txtDate" runat="server" Text='<%# Convert.ToDateTime(Eval("dateTime")).ToString("d") %>'></asp:TextBox>

查看此有用的站点以获取更多格式选项:

See this helpful site for more formatting options:

http://www.mikesdotnetting.com/Article.aspx?ArticleID=23

这篇关于asp.net 在 gridview 中格式化日期时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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