473,513 Members | 2,412 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

gridview, databinding, date and formatting problem

Hi all,

A simple problem that seems hard to solve...

I have a gridview with a date row.
<ItemTemplate>
<asp:Label ID="Label2" runat="server" Text='<%# Bind("time")
%>'></asp:Label>
</ItemTemplate>

Can I add some code that only the date will be shown and not the time?

Best regards,
Stijn

Nov 23 '06 #1
12 5924

ta******@gmail.com schreef:
Hi all,

A simple problem that seems hard to solve...

I have a gridview with a date row.
<ItemTemplate>
<asp:Label ID="Label2" runat="server" Text='<%# Bind("time")
%>'></asp:Label>
</ItemTemplate>

Can I add some code that only the date will be shown and not the time?

Best regards,
Stijn
It seems that when you "change this row to a templatefield" the
dataformat property is not available anymore. Not entirely unlogical
but can I format the strings of the row for let's say theItemTemplate?
Any help is greatly appreciated.

Nov 23 '06 #2
You can format your date in your query to return the date in the format you
want.

If using SQL Server: CONVERT(DATETIME, YourField, 1)

In my opinion, it is much easier to just return the data as you want it then
to send back something you don't need, in the case of a time on the date, and
then format it.

"ta******@gmail.com" wrote:
>
ta******@gmail.com schreef:
Hi all,

A simple problem that seems hard to solve...

I have a gridview with a date row.
<ItemTemplate>
<asp:Label ID="Label2" runat="server" Text='<%# Bind("time")
%>'></asp:Label>
</ItemTemplate>

Can I add some code that only the date will be shown and not the time?

Best regards,
Stijn

It seems that when you "change this row to a templatefield" the
dataformat property is not available anymore. Not entirely unlogical
but can I format the strings of the row for let's say theItemTemplate?
Any help is greatly appreciated.

Nov 23 '06 #3
"Mike Collins" <Mi*********@discussions.microsoft.comwrote in message
news:89**********************************@microsof t.com...
You can format your date in your query to return the date in the format
you
want.

If using SQL Server: CONVERT(DATETIME, YourField, 1)

In my opinion, it is much easier to just return the data as you want it
then
to send back something you don't need, in the case of a time on the date,
and
then format it.
I disagree totally.

For one thing, the CONVERT function in SQL Server has a fairly limited
number of options for dates - certainly nothing like the richness that is
available with the .ToString() method in .NET.

Secondly, it really isn't the job of the database layer to provide
formatting - that's for the presentation layer. Much better, IMO, to let SQL
Server return the native data to to the GUI (through the business layer(s),
as required) and let the GUI control how that data is displayed...
Nov 23 '06 #4

Mark Rae schreef:
"Mike Collins" <Mi*********@discussions.microsoft.comwrote in message
news:89**********************************@microsof t.com...
You can format your date in your query to return the date in the format
you
want.

If using SQL Server: CONVERT(DATETIME, YourField, 1)

In my opinion, it is much easier to just return the data as you want it
then
to send back something you don't need, in the case of a time on the date,
and
then format it.

I disagree totally.

For one thing, the CONVERT function in SQL Server has a fairly limited
number of options for dates - certainly nothing like the richness that is
available with the .ToString() method in .NET.

Secondly, it really isn't the job of the database layer to provide
formatting - that's for the presentation layer. Much better, IMO, to let SQL
Server return the native data to to the GUI (through the business layer(s),
as required) and let the GUI control how that data is displayed...
Thanks all for the replies.

Since I actually need it for more than date formatting, SQGformatting
is not really an option. I 'm really interested on how I can do this in
asp.net itself. Someone can help me here?

thanks in advance

Nov 23 '06 #5
For a normal bounded field it's easy;
1) Turn off html encoding
2) Do the formatting stuff on the specific format property like
{0:yyyy-MM-dd}

<ta******@gmail.comschreef in bericht
news:11**********************@j44g2000cwa.googlegr oups.com...
Hi all,

A simple problem that seems hard to solve...

I have a gridview with a date row.
<ItemTemplate>
<asp:Label ID="Label2" runat="server" Text='<%# Bind("time")
%>'></asp:Label>
</ItemTemplate>

Can I add some code that only the date will be shown and not the time?

Best regards,
Stijn

Nov 23 '06 #6
<ta******@gmail.comwrote in message
news:11**********************@m7g2000cwm.googlegro ups.com...
Since I actually need it for more than date formatting, SQGformatting
is not really an option. I 'm really interested on how I can do this in
asp.net itself. Someone can help me here?
pseudo-code:

DataSet objDS = new <populate a DataSet>;
DateTime dtmDate = objDS.GetDateTime(0);
string strDateAsText = String.Empty;
strDateAsText = dtmDate.ToString("dd MMM yyyy");
strDateAsText = dtmDate.ToString("dd MMM yyyy HH:mm");
strDateAsText = dtmDate.ToString("yyyyMMddHHmmss");

http://www.microsoft.com/globaldev/g.../wrg_date.mspx
Nov 23 '06 #7
I agree with Mike...

Plus, if you only return the data that you need, you save bandwidth. I know,
it may not be that much, but it adds up. IMO it is the same as not typing
Select * From Table, when Select MyOneField From Table is much better because
you are sending less data over the network.

I just recently used things like this because a page was taking too long to
finish downloading to a client's computer. It was not because the page was
broken. It was because of all the data that the page was displaying. By
making squeezing out every possible ounce of fat I could from the page, it
now loads over twice as fast.

So, if you do not need the information, why send it to only have to strip it
off anyway?

"Mark Rae" wrote:
"Mike Collins" <Mi*********@discussions.microsoft.comwrote in message
news:89**********************************@microsof t.com...
You can format your date in your query to return the date in the format
you
want.

If using SQL Server: CONVERT(DATETIME, YourField, 1)

In my opinion, it is much easier to just return the data as you want it
then
to send back something you don't need, in the case of a time on the date,
and
then format it.

I disagree totally.

For one thing, the CONVERT function in SQL Server has a fairly limited
number of options for dates - certainly nothing like the richness that is
available with the .ToString() method in .NET.

Secondly, it really isn't the job of the database layer to provide
formatting - that's for the presentation layer. Much better, IMO, to let SQL
Server return the native data to to the GUI (through the business layer(s),
as required) and let the GUI control how that data is displayed...
Nov 23 '06 #8
"Wannabe" <Wa*****@discussions.microsoft.comwrote in message
news:2F**********************************@microsof t.com...
Plus, if you only return the data that you need, you save bandwidth.
Oh for heaven's sake. A smalldatetime field in SQL Server is 4 bytes big
(look it up). Convert it to a varchar so that it returns "01 Nov 2006" - now
it's 11 bytes...
I know, it may not be that much, but it adds up. IMO it is the same as not
typing
Select * From Table, when Select MyOneField From Table is much better
because
you are sending less data over the network.
I fully agree - but what does that have to do with formatting dates...?
Nov 23 '06 #9
I fully agree - but what does that have to do with formatting dates...?

You do make a good point...let SQL Server do what it does best, get data.
Enough so, that I will be doing some reading on the subject, to see what the
best practice is. I was just trying to point out that you should be aware of
the amount of data that is being sent over the network, as a whole. So it was
just the concept I am trying to point out.

"Mark Rae" wrote:
"Wannabe" <Wa*****@discussions.microsoft.comwrote in message
news:2F**********************************@microsof t.com...
Plus, if you only return the data that you need, you save bandwidth.

Oh for heaven's sake. A smalldatetime field in SQL Server is 4 bytes big
(look it up). Convert it to a varchar so that it returns "01 Nov 2006" - now
it's 11 bytes...
I know, it may not be that much, but it adds up. IMO it is the same as not
typing
Select * From Table, when Select MyOneField From Table is much better
because
you are sending less data over the network.

I fully agree - but what does that have to do with formatting dates...?
Nov 23 '06 #10
Is the GridView databound? And if so, what do you bind it to?
Nov 24 '06 #11
Stijn, try this:

<ItemTemplate>
<asp:Label ID="Label2" runat="server" Text='<%# Eval("time", "{0:d}")
%>'></asp:Label>
</ItemTemplate>

ta******@gmail.com wrote:
Hi all,

A simple problem that seems hard to solve...

I have a gridview with a date row.
<ItemTemplate>
<asp:Label ID="Label2" runat="server" Text='<%# Bind("time")
%>'></asp:Label>
</ItemTemplate>

Can I add some code that only the date will be shown and not the time?

Best regards,
Stijn
Dec 27 '06 #12
Stijn, try this:

<ItemTemplate>
<asp:Label ID="Label2" runat="server" Text='<%# Eval("time", "{0:d}")
%>'></asp:Label>
</ItemTemplate>

Best regards,
EVgen

ta******@gmail.com wrote:
Hi all,

A simple problem that seems hard to solve...

I have a gridview with a date row.
<ItemTemplate>
<asp:Label ID="Label2" runat="server" Text='<%# Bind("time")
%>'></asp:Label>
</ItemTemplate>

Can I add some code that only the date will be shown and not the time?

Best regards,
Stijn
Dec 27 '06 #13

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

Similar topics

3
6209
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...
4
2607
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
13725
by: NateDawg | last post by:
I'm reposting this. I'm kinda in a bind untill i get this figured out, so if anyone has some input it would sure help me out. Ok, I’ve noticed a few gridview problems floating around the forum. Everyone wants to do a java confirmation box when a user clicks the delete button. Fair enough, basic user design rules state that you should...
4
13476
by: Jim Katz | last post by:
I have an application that updates a strongly typed data set at run time. I'd like to dynamically create a table that connects to a run time data table. For displaying the data, this works well. I just set the GridView.DataSource once, and call DataBind(); I'd like to drive the application from the GridView control, by including command...
5
1624
by: Edwin Knoppert | last post by:
I want to format a hyperlink bounded field. Fields *could* have http:// *or not* in the websitename entered. I want to format this to http://website.com otherwise i'll get a relative link. I know the itemtemplate workarounds or through row events. It's that i'm trying to use the internal format stuff itself. Is this possible?
3
2085
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>
9
11816
by: J055 | last post by:
Hi I'm trying to get an instance of UserLists to persist after it's been used by the GridView. I understand from the documentation that The OnObjectCreated event allows access to the instance. I have a TotalUsers property which is initiated when the GetUsers method is called, however it is empty after the OnObjectCreated event runs. What am...
1
3224
by: Giovanni | last post by:
Dear Friends/Gurus, I have exhausted myself and have yet no solution to the following: I have an ASP.NET 2.0 Survey type application. On a page, I have placed a GridView which is bound to an SQLDataSource. The GridView contains 3 columns: QuestionNumber, QuestionText, and a TemplateField. Each row in the GridView represents a...
4
8314
by: Ken Wigle | last post by:
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...
0
7177
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...
0
7394
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
7542
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
5701
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...
1
5100
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...
0
3248
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...
0
3237
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
811
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
470
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.