gridview, databinding, date and formatting problem

T

tarscher

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
 
T

tarscher

(e-mail address removed) 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.
 
G

Guest

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.
 
M

Mark Rae

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...
 
T

tarscher

Mark Rae schreef:
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
 
E

Edwin Knoppert

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}
 
M

Mark Rae

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/getWR/steps/wrg_date.mspx
 
G

Guest

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?
 
M

Mark Rae

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...?
 
G

Guest

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.
 
E

EVgen

Stijn, try this:

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

EVgen

Stijn, try this:

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

Best regards,
EVgen
 

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,995
Messages
2,570,226
Members
46,815
Latest member
treekmostly22

Latest Threads

Top