formatting problem with dynamic gridview

G

Guest

All,

I would be very grateful for any help on this question. I have an
application in asp.net 2.0 where I dynamically create a datatable and then
bind that to a gridview. Unfortunately, the date column always shows the
date and time while I just want the short date. I have tried applying a
format string {0:d} but to no avail. I saw a lot of posts regarding the
htmlencode property but I do not know how to turn that off for a dynamically
created column - can it be done? If so, that may solve my problem. Here is
a small snapshot of what I am doing:

Dim dcLeaveDate As New DataColumn("LeaveDate", GetType(Date))
Dim dtleaveformdays As New DataTable

dtleaveformdays.Columns.Add(dcLeaveDate)
gvDays.DataSource = dtleaveformdays
gvDays.DataBind()

gvDays is my gridview and I cannot get the date column to format. I even
tried doing this in the rowcreated function:

Protected Sub gvDays_RowCreated........
e.Row.Cells(0).Text = String.Format("{0:d}", e.Row.Cells(0).Text)

Please help - I need to do this dynamically and keep track of all changes
they make before they actually submit the data. At that point, I will save
the datatable to a sql table. In the meantime, dynamic formatting is giving
me a headache (-:

Thanks,
Ken
 
K

Kyle K.

Using "{0:dd-MMM-yyyy}" will give you the date as a 2-digit day,
3-letter month abbreviation, and 4-digit year separated by dashes ('-').

Of course there are other options. Check the MSDN docs for Date & Time
formatting.

-={ Kyle }=-
 
G

Guest

That does not help - I tried that formatting instead of {0:d} but it does not
affect the gridview. The gridview ignores all formatting and I think it is
because of htmlencoding which I have not found a way to turn off when using
dynamic columns.

I appreciate your help - I will try anything to try and get this working. I
seem to spend much more time on little things that should be simple to do
than the actual project itself.

ken
 
K

Ken Cox [Microsoft MVP]

Hi Ken,

If you don't know where the DateTime is coming from because the GridView is
dynamic, you have to dig into every cell in the row to find a DateTime and
apply the formatting to the text. It's a pain, but it can be done.

Here's a little sample that shows you the concept.

Let us know if this helps?

Ken
Microsoft MVP [ASP.NET]

<%@ Page Language="VB" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<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]
' August 30/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 _
("LeaveDate", GetType(DateTime)))
Dim i As Integer
For i = 0 To 5
dr = dt.NewRow()
dr(0) = Now.AddDays(i)
dt.Rows.Add(dr)
Next i
Return dt
End Function
</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Format Dates in Dynamic GridView</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:gridview id="GridView1" runat="server"
onrowdatabound="GridView1_RowDataBound">
</asp:gridview>
</div>
</form>
</body>
</html>
 
G

Guest

Thank you very much! The rowdatabound routine worked perfectly! I really
appreciate it.

Ken

Ken Cox said:
Hi Ken,

If you don't know where the DateTime is coming from because the GridView is
dynamic, you have to dig into every cell in the row to find a DateTime and
apply the formatting to the text. It's a pain, but it can be done.

Here's a little sample that shows you the concept.

Let us know if this helps?

Ken
Microsoft MVP [ASP.NET]

<%@ Page Language="VB" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<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]
' August 30/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 _
("LeaveDate", GetType(DateTime)))
Dim i As Integer
For i = 0 To 5
dr = dt.NewRow()
dr(0) = Now.AddDays(i)
dt.Rows.Add(dr)
Next i
Return dt
End Function
</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Format Dates in Dynamic GridView</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:gridview id="GridView1" runat="server"
onrowdatabound="GridView1_RowDataBound">
</asp:gridview>
</div>
</form>
</body>
</html>

Ken Wigle said:
All,

I would be very grateful for any help on this question. I have an
application in asp.net 2.0 where I dynamically create a datatable and then
bind that to a gridview. Unfortunately, the date column always shows the
date and time while I just want the short date. I have tried applying a
format string {0:d} but to no avail. I saw a lot of posts regarding the
htmlencode property but I do not know how to turn that off for a
dynamically
created column - can it be done? If so, that may solve my problem. Here
is
a small snapshot of what I am doing:

Dim dcLeaveDate As New DataColumn("LeaveDate", GetType(Date))
Dim dtleaveformdays As New DataTable

dtleaveformdays.Columns.Add(dcLeaveDate)
gvDays.DataSource = dtleaveformdays
gvDays.DataBind()

gvDays is my gridview and I cannot get the date column to format. I even
tried doing this in the rowcreated function:

Protected Sub gvDays_RowCreated........
e.Row.Cells(0).Text = String.Format("{0:d}", e.Row.Cells(0).Text)

Please help - I need to do this dynamically and keep track of all changes
they make before they actually submit the data. At that point, I will
save
the datatable to a sql table. In the meantime, dynamic formatting is
giving
me a headache (-:

Thanks,
Ken
 
Joined
Jul 12, 2007
Messages
1
Reaction score
0
formating date in dynamic gridview

hi
i been working on this since so long and then i found your post which made me happy but i m having problem here, i used your code to formate date in my gridview in C# and its giving me problem saying
"CS0266: Cannot implicitly convert type 'object' to 'System.Data.DataRowView'"
please help me on that. it will solve all my probs.
appreciate your help
thank you


Ken Cox [Microsoft MVP] said:
Hi Ken,

If you don't know where the DateTime is coming from because the GridView is
dynamic, you have to dig into every cell in the row to find a DateTime and
apply the formatting to the text. It's a pain, but it can be done.

Here's a little sample that shows you the concept.

Let us know if this helps?

Ken
Microsoft MVP [ASP.NET]

<%@ Page Language="VB" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<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]
' August 30/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 _
("LeaveDate", GetType(DateTime)))
Dim i As Integer
For i = 0 To 5
dr = dt.NewRow()
dr(0) = Now.AddDays(i)
dt.Rows.Add(dr)
Next i
Return dt
End Function
</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Format Dates in Dynamic GridView</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:gridview id="GridView1" runat="server"
onrowdatabound="GridView1_RowDataBound">
</asp:gridview>
</div>
</form>
</body>
</html>

"Ken Wigle" <[email protected]> wrote in message
news:[email protected]...
> All,
>
> I would be very grateful for any help on this question. I have an
> application in asp.net 2.0 where I dynamically create a datatable and then
> bind that to a gridview. Unfortunately, the date column always shows the
> date and time while I just want the short date. I have tried applying a
> format string {0:d} but to no avail. I saw a lot of posts regarding the
> htmlencode property but I do not know how to turn that off for a
> dynamically
> created column - can it be done? If so, that may solve my problem. Here
> is
> a small snapshot of what I am doing:
>
> Dim dcLeaveDate As New DataColumn("LeaveDate", GetType(Date))
> Dim dtleaveformdays As New DataTable
>
> dtleaveformdays.Columns.Add(dcLeaveDate)
> gvDays.DataSource = dtleaveformdays
> gvDays.DataBind()
>
> gvDays is my gridview and I cannot get the date column to format. I even
> tried doing this in the rowcreated function:
>
> Protected Sub gvDays_RowCreated........
> e.Row.Cells(0).Text = String.Format("{0:d}", e.Row.Cells(0).Text)
>
> Please help - I need to do this dynamically and keep track of all changes
> they make before they actually submit the data. At that point, I will
> save
> the datatable to a sql table. In the meantime, dynamic formatting is
> giving
> me a headache (-:
>
> Thanks,
> Ken
>
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,994
Messages
2,570,223
Members
46,813
Latest member
lawrwtwinkle111

Latest Threads

Top