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 gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

format DataGrid columns

Status
Not open for further replies.

HomerJS

Programmer
Jun 25, 2001
86
US
I'm populating a datagrid from our HP using ASP.NET. I want to change the format from YYYYMMDD to MM/DD/YYYY inside the DataGrid. How do I do this?

Also how do I format amounts inside a DataGrid? ($#,###.##)
 
It depends on the way you are using your datagrid.

If you have bound columns:
On the property builder, select the bound column and add '{0:MM/dd/yyyy}' on the data formating expression.

If you have templated columns with a control to write the date on (using databindings):
On the databinding of the control add 'DataBinder.Eval(Container, "DataItem.MyDate", "{0: MM/dd/yyyy}")'

For currency use the same method:
{0:($#,###.##)}

Hope this helps.
NetAngel
 
Thanks NetAngel. That did fix my formatting problem with bound columns.

If I can pick your brain a little more, how can I display negative numbers with paranthesis instead of the negative sign infront?

Also how can I get at a specific cell to input the date the correct way? I'm having issues trying to fomat the date. The format is YYYYMMDD and I want to change it to MM/DD/YYYY. Any good suggestions as to how to do this?

Thanks again.
 
To format negative numbers using paranthesis use the following format:
{0:$#,###.##;($#,###.##)}

To format the data of a column on the 'MM/DD/YYYY' format, use:
{0: MM/dd/yyyy}
I'm assuming u are returning a date type field form the database.
NetAngel
 
The problem I'm having with the date column is I'm importing a date that's in the format: YYYYMMDD I tried using {0: MM/dd/yyyy} but the grid did not display and got the message: 'Format specifier was invalid.'

Is there a way in code to input data into certain columns? If so then I could format the dates myself.
 
You can use this trick to format a data column yourself.

In the html part of your page where the column is databound use this syntax[color]
<%# FormatDate(DataBinder.Eval Container, &quot;DataSource.DataMember&quot;)) %>[/color]
The bold text is the name of a function you need to code in the code-behind page. Something like so..
Protected Function FormatDate(ByVal arg As Object) As String
Dim output As String
If arg Is Nothing Then
output = 'default output for blanks
ElseIf IsDBNull(arg) Then
output = 'same as above
Else
output = 'format arg here
End If
Return output
End Function
That'l do donkey, that'l do
[bravo] Mark
If you are unsure of forum etiquette check here faq796-2540
 
How is the date saved in the database? If it is saved as datetime (or date, depending on the db), u can use the format code I gave u.

If you are saving it as a string (char, varchar), I believe you have to build the format yourself with string functions (mid, left, right).

Tell me if you need help with those string functions.
NetAngel
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top