Coffee and Code

The Life and Times of Jeff Woodman

Home >> Adventures in Databinding

Adventures in Databinding

Posted: 02/10/2007 06:23 PM | permaLink

Tags: development asp.net tech

Here are a couple of ASP.Net databinding / formatting tricks I've picked up recently:

1. I recently had to bind to a telephone number in a GridView that was stored in the database as a 10-character string. I ran into problems trying to use a DataFormatString property to format the output as (555) 555.5555. Turns out that format strings generally work better with numbers than strings.... duh.

C# Solution: Convert.ToInt64(string).ToString('(000) 000.0000') did the trick. I can't remember where I found the solution, but it's really useful! Sure beats writing a custom method to re-format the number.

2. When using a BoundField to display a DateTime, the DataFormatString property will only work when the HtmlEncode property is set to false.