Wednesday, February 21, 2007

This is cool


 

Posted this using OneNote 2007 and Word 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();

}
}

Started...

I had nothing to do.. I had been wanting to blog for a long time now.. And fate just tied these two together. Here goes my first post.