GridView date format problem

Discussion in 'ASP .Net Datagrid Control' started by Bob Phillips, Jul 27, 2006.

  1. Bob Phillips

    Bob Phillips Guest

    Hello All:
    I have a GridView in a user control that has its datasource assigned
    dynamically. It's working well, however, date values are displaying
    mm/dd/yyyy hh:mm:ss and I only want a short date format. I don't know
    which column will hold a date prior to runtime. Is there any way to
    set it on the fly?
    -Bob
    Bob Phillips, Jul 27, 2006
    #1
    1. Advertising

  2. Hi Bob,

    Interesting challenge. It took a while but I came up with something that
    *seems* to work. See the comments inline in the code sample

    Could you give the code below a test and see if it does what you want?

    Let us know?

    Ken
    Microsoft MVP [ASP.NET]


    <%@ control language="VB" classname="dgridusr" %>
    <script runat="server">
    Protected Sub Page_Load _
    (ByVal sender As Object, ByVal e As System.EventArgs)
    If Not IsPostBack Then
    GridView1.DataSource = CreateDataSource()
    GridView1.DataBind()
    End If
    End Sub

    Protected Sub GridView1_RowDataBound _
    (ByVal sender As Object, ByVal e As _
    System.Web.UI.WebControls.GridViewRowEventArgs)
    ' Routine to display an unknown DateTime column
    ' as a short date at runtime
    ' By Ken Cox [Microsoft MVP]
    ' July 28/06
    '
    ' Make sure this is a datarow being bound
    If e.Row.RowType = DataControlRowType.DataRow Then
    ' Create variables to hold values
    Dim dtview As System.Data.DataRowView
    Dim dt As DateTime
    Dim intCounter As Integer
    ' Get the contents of the current row
    ' as a DataRowView
    dtview = e.Row.DataItem
    ' Loop through the individual values in the
    ' DataRowView's ItemArray
    For intCounter = 0 To _
    dtview.Row.ItemArray.Length - 1
    ' Check if the current value is
    ' a System.DateTime type
    If TypeOf dtview.Row.Item(intCounter) Is _
    System.DateTime Then
    ' If it is a DateTime, cast it as such
    ' into the variable
    dt = dtview.Row.Item(intCounter)
    ' Set the text of the current cell
    ' as the short date representation
    ' of the datetime
    e.Row.Cells(intCounter).Text = _
    dt.ToShortDateString
    End If
    Next
    End If
    End Sub

    Function CreateDataSource() As Data.DataTable
    ' Provides some dummy data
    Dim dt As New Data.DataTable
    Dim dr As Data.DataRow
    dt.Columns.Add(New Data.DataColumn _
    ("IntegerValue", GetType(Int32)))
    dt.Columns.Add(New Data.DataColumn _
    ("StringValue", GetType(String)))
    dt.Columns.Add(New Data.DataColumn _
    ("CurrencyValue", GetType(Double)))
    dt.Columns.Add(New Data.DataColumn _
    ("DateTime", GetType(DateTime)))
    Dim i As Integer
    For i = 0 To 5
    dr = dt.NewRow()
    dr(0) = i
    dr(1) = "Item " + i.ToString()
    dr(2) = 1.23 * (i + 1)
    dr(3) = Now.AddDays(i)
    dt.Rows.Add(dr)
    Next i
    Return dt
    End Function
    </script>

    <asp:gridview id="GridView1"
    runat="server"
    onrowdatabound="GridView1_RowDataBound">
    </asp:gridview>

    "Bob Phillips" <> wrote in message
    news:...
    > Hello All:
    > I have a GridView in a user control that has its datasource assigned
    > dynamically. It's working well, however, date values are displaying
    > mm/dd/yyyy hh:mm:ss and I only want a short date format. I don't know
    > which column will hold a date prior to runtime. Is there any way to
    > set it on the fly?
    > -Bob
    >
    Ken Cox [Microsoft MVP], Jul 29, 2006
    #2
    1. Advertising

  3. Bob Phillips

    Bob Phillips Guest

    Thanks so much, Ken!! Looks like it will do the trick.
    -Bob

    Ken Cox [Microsoft MVP] wrote:
    > Hi Bob,
    >
    > Interesting challenge. It took a while but I came up with something that
    > *seems* to work. See the comments inline in the code sample
    >
    > Could you give the code below a test and see if it does what you want?
    >
    > Let us know?
    >
    > Ken
    > Microsoft MVP [ASP.NET]
    >
    >
    > <%@ control language="VB" classname="dgridusr" %>
    > <script runat="server">
    > Protected Sub Page_Load _
    > (ByVal sender As Object, ByVal e As System.EventArgs)
    > If Not IsPostBack Then
    > GridView1.DataSource = CreateDataSource()
    > GridView1.DataBind()
    > End If
    > End Sub
    >
    > Protected Sub GridView1_RowDataBound _
    > (ByVal sender As Object, ByVal e As _
    > System.Web.UI.WebControls.GridViewRowEventArgs)
    > ' Routine to display an unknown DateTime column
    > ' as a short date at runtime
    > ' By Ken Cox [Microsoft MVP]
    > ' July 28/06
    > '
    > ' Make sure this is a datarow being bound
    > If e.Row.RowType = DataControlRowType.DataRow Then
    > ' Create variables to hold values
    > Dim dtview As System.Data.DataRowView
    > Dim dt As DateTime
    > Dim intCounter As Integer
    > ' Get the contents of the current row
    > ' as a DataRowView
    > dtview = e.Row.DataItem
    > ' Loop through the individual values in the
    > ' DataRowView's ItemArray
    > For intCounter = 0 To _
    > dtview.Row.ItemArray.Length - 1
    > ' Check if the current value is
    > ' a System.DateTime type
    > If TypeOf dtview.Row.Item(intCounter) Is _
    > System.DateTime Then
    > ' If it is a DateTime, cast it as such
    > ' into the variable
    > dt = dtview.Row.Item(intCounter)
    > ' Set the text of the current cell
    > ' as the short date representation
    > ' of the datetime
    > e.Row.Cells(intCounter).Text = _
    > dt.ToShortDateString
    > End If
    > Next
    > End If
    > End Sub
    >
    > Function CreateDataSource() As Data.DataTable
    > ' Provides some dummy data
    > Dim dt As New Data.DataTable
    > Dim dr As Data.DataRow
    > dt.Columns.Add(New Data.DataColumn _
    > ("IntegerValue", GetType(Int32)))
    > dt.Columns.Add(New Data.DataColumn _
    > ("StringValue", GetType(String)))
    > dt.Columns.Add(New Data.DataColumn _
    > ("CurrencyValue", GetType(Double)))
    > dt.Columns.Add(New Data.DataColumn _
    > ("DateTime", GetType(DateTime)))
    > Dim i As Integer
    > For i = 0 To 5
    > dr = dt.NewRow()
    > dr(0) = i
    > dr(1) = "Item " + i.ToString()
    > dr(2) = 1.23 * (i + 1)
    > dr(3) = Now.AddDays(i)
    > dt.Rows.Add(dr)
    > Next i
    > Return dt
    > End Function
    > </script>
    >
    > <asp:gridview id="GridView1"
    > runat="server"
    > onrowdatabound="GridView1_RowDataBound">
    > </asp:gridview>
    >
    > "Bob Phillips" <> wrote in message
    > news:...
    > > Hello All:
    > > I have a GridView in a user control that has its datasource assigned
    > > dynamically. It's working well, however, date values are displaying
    > > mm/dd/yyyy hh:mm:ss and I only want a short date format. I don't know
    > > which column will hold a date prior to runtime. Is there any way to
    > > set it on the fly?
    > > -Bob
    > >
    Bob Phillips, Aug 1, 2006
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Brian Candy
    Replies:
    2
    Views:
    1,150
    Janaka
    Feb 18, 2004
  2. DC
    Replies:
    5
    Views:
    2,994
    Brendan Reynolds
    Jul 1, 2005
  3. Peter Grison

    Date, date date date....

    Peter Grison, May 28, 2004, in forum: Java
    Replies:
    10
    Views:
    3,229
    Michael Borgwardt
    May 30, 2004
  4. Jules Wensley

    Gridview Date Format Problem

    Jules Wensley, Dec 17, 2005, in forum: ASP .Net Datagrid Control
    Replies:
    6
    Views:
    160
  5. Une bévue

    problem with date and date/format

    Une bévue, Mar 10, 2006, in forum: Ruby
    Replies:
    3
    Views:
    106
    Une bévue
    Mar 12, 2006
Loading...

Share This Page