HighTechTalks DotNet Forums  

GridView date format problem

ASP.net Data Grid Control microsoft.public.dotnet.framework.aspnet.datagridcontrol


Discuss GridView date format problem in the ASP.net Data Grid Control forum.



Reply
 
Thread Tools Search this Thread Display Modes
  #1  
Old   
Bob Phillips
 
Posts: n/a

Default GridView date format problem - 07-27-2006 , 03:56 PM






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


Reply With Quote
  #2  
Old   
Ken Cox [Microsoft MVP]
 
Posts: n/a

Default Re: GridView date format problem - 07-29-2006 , 12:04 AM






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" <phil1630 (AT) bellsouth (DOT) net> wrote

Quote:
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




Reply With Quote
  #3  
Old   
Bob Phillips
 
Posts: n/a

Default Re: GridView date format problem - 08-01-2006 , 02:18 PM



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

Ken Cox [Microsoft MVP] wrote:
Quote:
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" <phil1630 (AT) bellsouth (DOT) net> wrote in message
news:1154030179.908776.70110 (AT) i3g2000cwc (DOT) googlegroups.com...
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



Reply With Quote
Reply




Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.4
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.