Wednesday, February 21, 2007

Custom formatting in DataGridView

I have a dataset that is returned by a web service. It contains a date column, among other columns, and this date is an UTC date(so, GMT time). Now, only when i display it in a datagridview, i need to display the local time depending on the user's local regional settings. Here is what I ended up doing....

// Bind the Cell formatting event
dgvGrid.CellFormatting += new DataGridViewCellFormattingEventHandler(dgvGrid_CellFormatting);


// Formatting event handler
void dgvGrid_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)

{
if( e.Value.GetType() == typeof(DateTime) )

{
e.CellStyle.Format = "G";
// This is the key. Converts GMT time to local time by adding the offset.
e.Value = ((DateTime)e.Value).ToLocalTime();

}
}

3 comments:

Anonymous said...

Your blog keeps getting better and better! Your older articles are not as good as newer ones you have a lot more creativity and originality now keep it up!

Anonymous said...

[B]NZBsRus.com[/B]
Dont Bother With Laggin Downloads With NZB Downloads You Can Rapidly Find HD Movies, Games, MP3 Singles, Software & Download Them @ Accelerated Speeds

[URL=http://www.nzbsrus.com][B]Newsgroup[/B][/URL]

Anonymous said...

It isn't hard at all to start making money online in the underground world of [URL=http://www.www.blackhatmoneymaker.com]blackhat seo software[/URL], Don’t feel silly if you have no clue about blackhat marketing. Blackhat marketing uses little-known or not-so-known avenues to build an income online.