Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Alternate to isDate()?

Status
Not open for further replies.

mdProgrammer

Programmer
Oct 2, 2004
71
US
I have an unbound (dynamic) datagrid which sometimes has a column with a date field that needs to be formatted to show as '1/1/2009' instead of '1/1/2009 12:00:00 AM'. This works, however, it's when I have another column with values like "11-10", "11-11" (these are special code fields that the user understands), the isDate() function treats it as a date. I have the following code in my ItemDataBound event -

Code:
        For i As Integer = 0 To e.Item.Cells.Count - 1
            If IsDate(e.Item.Cells(i).Text) Then
                Dim dte As Date = e.Item.Cells(i).Text
                e.Item.Cells(i).Text = DatePart(DateInterval.Month, dte) & "/" & DatePart(DateInterval.Day, dte) & "/" & DatePart(DateInterval.Year, dte)
            End If
        Next

It works great until it sees something like '11-10' (even something like 11.10, 11!10, 11~10, etc., will be changed to a date) and turns it into 11/10/2009. For now, I have to add this code for it to work -

Code:
And (e.Item.Cells(i).Text.Length >= 20)

Is there another way to do this?
 
i question the approach to this. Cells[index].Text is about the rendered HTML, not the data which created that HTML. You are modifying the presentation too late in the life cycle.

rather you should wire a GridViewRowDataBound event handler and manipulate the value for display. there are plenty of examples about this on the web and in this forum.

Jason Meckley
Programmer
Specialty Bakers, Inc.
 
This is an ASP.NET 1.1 application (I haven't converted it to 2.0 yet - it's a long story), so there's no GridViewRowDataBound event handler.
 
Also, the reason I'm changing how the date looks is that if I don't, it looks like this:

1/5/2009 12:00:00 AM
1/8/2009 12:00:00 AM
1/20/2009 12:00:00 AM

and so on.

I've tried removing the 12:00:00 AM during the databind, but then when it's sorted, it's out of order. i.e.,
(This is in ascending order) That's why I'm sorting it by the default date, then changing the HTML output so that 12:00:00 AM doesn't show.

01/04/2005
01/04/2007
01/04/2008
01/05/2007
01/06/2006
01/06/2006

 
the sort is wrong because it's sorting strings, not dates. you can format the date like this
Code:
//january 2nd, 2008 11:10:55 am
DateTime january_2nd_2008_11_10_and_55_seconds_am = new DateTime(2008, 1, 2, 11, 10, 55);
string formatted_date = january_2nd_2008_11_10_and_55_seconds_am.ToString("MM-dd-yy");
with a GridView you may even be able to format the value directly from the grid. something like
Code:
DataValue="name of column" DataFormat="MM-dd-yy"

Jason Meckley
Programmer
Specialty Bakers, Inc.
 
Not sure how stripping off the time would affect the sort, but since we can't see your code, there is no way to tell.

This is the way I format the text in a cell in a gridview or datagrid. Use the ItemDataBound event as Jason stated:
Code:
 e.Item.Cells(<cell index>).Text = String.Format("{0;d}", e.Item.Cells(<cell index>).Text)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top