473,747 Members | 2,849 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

formatting problem with dynamic gridview

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("Lea veDate", GetType(Date))
Dim dtleaveformdays As New DataTable

dtleaveformdays .Columns.Add(dc LeaveDate)
gvDays.DataSour ce = 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_RowCreat ed........
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

Aug 30 '06 #1
4 8331
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 }=-
Ken Wigle wrote:
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("Lea veDate", GetType(Date))
Dim dtleaveformdays As New DataTable

dtleaveformdays .Columns.Add(dc LeaveDate)
gvDays.DataSour ce = 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_RowCreat ed........
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
Aug 30 '06 #2
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
"Kyle K." wrote:
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 }=-
Ken Wigle wrote:
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("Lea veDate", GetType(Date))
Dim dtleaveformdays As New DataTable

dtleaveformdays .Columns.Add(dc LeaveDate)
gvDays.DataSour ce = 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_RowCreat ed........
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
Aug 30 '06 #3
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.dt d">
<script runat="server">
Protected Sub Page_Load _
(ByVal sender As Object, ByVal e As System.EventArg s)
If Not IsPostBack Then
GridView1.DataS ource = CreateDataSourc e()
GridView1.DataB ind()
End If
End Sub
Protected Sub GridView1_RowDa taBound _
(ByVal sender As Object, ByVal e As _
System.Web.UI.W ebControls.Grid ViewRowEventArg s)
' 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 = DataControlRowT ype.DataRow Then
' Create variables to hold values
Dim dtview As System.Data.Dat aRowView
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.Item Array.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(int Counter).Text = _
dt.ToShortDateS tring
End If
Next
End If
End Sub
Function CreateDataSourc e() 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(DateTim e)))
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_RowD ataBound">
</asp:gridview>
</div>
</form>
</body>
</html>

"Ken Wigle" <Ke******@discu ssions.microsof t.comwrote in message
news:6E******** *************** ***********@mic rosoft.com...
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("Lea veDate", GetType(Date))
Dim dtleaveformdays As New DataTable

dtleaveformdays .Columns.Add(dc LeaveDate)
gvDays.DataSour ce = 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_RowCreat ed........
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

Aug 31 '06 #4
Thank you very much! The rowdatabound routine worked perfectly! I really
appreciate it.

Ken

"Ken Cox [Microsoft MVP]" wrote:
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.dt d">
<script runat="server">
Protected Sub Page_Load _
(ByVal sender As Object, ByVal e As System.EventArg s)
If Not IsPostBack Then
GridView1.DataS ource = CreateDataSourc e()
GridView1.DataB ind()
End If
End Sub
Protected Sub GridView1_RowDa taBound _
(ByVal sender As Object, ByVal e As _
System.Web.UI.W ebControls.Grid ViewRowEventArg s)
' 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 = DataControlRowT ype.DataRow Then
' Create variables to hold values
Dim dtview As System.Data.Dat aRowView
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.Item Array.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(int Counter).Text = _
dt.ToShortDateS tring
End If
Next
End If
End Sub
Function CreateDataSourc e() 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(DateTim e)))
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_RowD ataBound">
</asp:gridview>
</div>
</form>
</body>
</html>

"Ken Wigle" <Ke******@discu ssions.microsof t.comwrote in message
news:6E******** *************** ***********@mic rosoft.com...
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("Lea veDate", GetType(Date))
Dim dtleaveformdays As New DataTable

dtleaveformdays .Columns.Add(dc LeaveDate)
gvDays.DataSour ce = 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_RowCreat ed........
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


Aug 31 '06 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
6221
by: washoetech | last post by:
I have a gridview control. In this grid view there is a column for the price of an item. Some of the prices have a dollar sign in front of it and some dont. How do I get rid of the dollar sign if it is in front of the value? My guess would be to use a template column but I dont know how to go about this. Any ideas? Below is an example of what the data looks like raw from the database: $456.95 200.89
1
4577
by: euan | last post by:
HI Guys, I have bee using conditional formatting in the datagrid recently and I am moving over to framework 2.0 and noticed the datagrid has been replaced by the gridview. So, I would like to do conditional formatting on the gridview but it doesnt have the same methods. Currently in my datagrid I have this (this is just hacked together randomly but gives you the idea):
4
2616
by: Nalaka | last post by:
Hi, I have two questions about gridViews. 1. How can I intercept the row/column values at loading to change values? 2. After I update a row (using default update functionality), how can I re-format the updated row fields. I have looked at gridView.rowUpdated method, but cannot figure out how....
3
2096
by: Simon Harvey | last post by:
Hi all, I'm having problems getting my date to format. Someone told me that with the GridView, you need to use a TemplateColumn and not a BoundColumn when displaying dates. Given that, can anyone see what the problem is with the following code.... <asp:TemplateField HeaderText="Departure Date"> <ItemTemplate>
3
2399
by: CSharpguy | last post by:
I'm trying to format the GridView on my web form and its not working. for money I have {0:C} and for date fields, I have {0:d} and the format isn't changing, what else do I need to do to get the formatting working in the gridView?
1
1871
by: AmitKu | last post by:
I've got the Gridview going at full speed, and I've enabled editing, but when I click on the "edit" button, this ugly editing UI comes up. Well it's not ugly, but it's not great either. How do I change the font/font size of the text thats being edited? Thanks in advance, Amit
2
2087
by: pgonzo | last post by:
In my initial tests with the GridView control, I cannot understand why the DataFormatString attribute has no affect on the column formatting. The 'creation_date' field is being reported as "3/30/02 14:23:28". I want to display it as "03/30/02". I've included the code below. Can anyone offer an explanation? <asp:SqlDataSource ID="DD_Issues" runat="server"
1
2620
by: =?Utf-8?B?cGVsZWdrMQ==?= | last post by:
i have a GridView which i can Page on (go to page 2,10,...) the thing is that i have a dynamic query which set the GridView from starts on page 1. the thing is that when i start to go over the pages - the dynamic query isnt saved and i cant go to the real page 4 for example of the dynamic query,rather i go to page 4 of the original basic query
2
2414
by: tomh2099 | last post by:
Hi, Hi, I have an ASP.NET 2005 application (using VB) with a GridView control that needs to have the last 5 or 6 rows in Bold or maybe some other special formatting. Most of the rows show monthly info but the last few show other information. The gridview is bound to a stored procedure that returns data something like this: 'Jan 2007', 0 'Feb 2007', 8
0
8818
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9354
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9307
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8233
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6790
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6067
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4588
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4860
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
2771
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.