DateTime Problem in GridView/DataList

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • necro1000
    New Member
    • Apr 2008
    • 12

    DateTime Problem in GridView/DataList

    Hi,

    I am using an MS SQL Database with a table that has a DateTime column stored in it.

    When I try to bind the data inside this table to a GridView and/or DataList the date shows up as (for example) '2010-03-03T00:00:00+01: 00'.

    In my GridView I tried to format that datetime by using the following 'BoundField' tag;

    <asp:BoundFie ld DataField="News DateEntered" HeaderText="Dat e Entered" DataFormatStrin g = "{0:dd/MM/yyyy}" HtmlEncode="Fal se" />

    This is still wielding no results and I have tried to recreate the table and so on. Does anyone have any idea what my problem could be please?

    Thanks
  • CroCrew
    Recognized Expert Contributor
    • Jan 2008
    • 564

    #2
    Can you show the code that you are using to bind the data so we can help. Also, in the database how is that being stored? String, dataTime, or other?

    CroCrew~

    Comment

    • CroCrew
      Recognized Expert Contributor
      • Jan 2008
      • 564

      #3
      Without seeing your code it is hard to help. Here is an example that might help you out a bit.

      Code:
      <%@ Page Language="VB" AutoEventWireup="false" CodeFile="Default10.aspx.vb" Inherits="Default10" %>
      <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
      <html xmlns="http://www.w3.org/1999/xhtml">
          <head runat="server">
              <title></title>
          </head>
          <body>
              <form id="form1" runat="server">
                  <div>
                      <asp:DataGrid ID="Grid" runat="server" AutoGenerateColumns="False" GridLines="None">
                          <Columns>
                              <asp:BoundColumn DataField="NewsDateEntered" HeaderText="Date Entered" DataFormatString="{0:dd/MM/yyyy}" />
                          </Columns>
                      </asp:DataGrid>            
                  </div>
              </form>
          </body>
      </html>
      Code:
      Imports System.Data
      
      Partial Class Default10
          Inherits System.Web.UI.Page
      
          Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
              Dim mydatatable As New DataTable
              mydatatable.Columns.Add("NewsDateEntered", Type.GetType("System.String"))
              Dim myrow As DataRow
      
              myrow = mydatatable.NewRow
              myrow("NewsDateEntered") = DateTime.Parse("2010-03-03T00:00:00+01:00")
              mydatatable.Rows.Add(myrow)
      
              Grid.DataSource = mydatatable
              Grid.DataBind()
          End Sub
      End Class
      We could help out more if we could see all your code.

      Happy Coding,
      CroCrew~

      Comment

      Working...