Formatting Strings in a DataGrid


In my opinion, one of the coolest .net controls is the DataGrid.  There is sooo much you can do with this puppy.  One of the things I like most about it is how you can choose to control or not control how data is rendered.  Which brings me to the topic of this post, how to format a string to show the date how you want it to appear.

The Datagrid will display data without any formatting if you are using bound columns.  However; there will be be situations when you need to display data in a certain custom format - say like a date string. To do custom formatting, you need to utilize the template column instead of the bound column.

Here’s how you would format a date in a template column so that your date would appear as 4/14/2006:

DataBinder.Eval(Container.DataItem,"RaisedDate","{0:MM/dd/yyyy}"

For more examples on how to format strings, check out this blog post that frankly does a better job than .NET’s documentation.  http://blog.stevex.net/index.php/string-formatting-in-csharp/

Comments (0)

Skip to main content