Asp.net formatting dateTime in gridview

asp.netgridview

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 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!

Best Solution

Try:

<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

Related Question