Home > C# > Change cell formatting on DataGridView

Change cell formatting on DataGridView

How to change the visual presentation of the scope of DataGridView. If in a particular field of the table in the database have such a unix timestamp value like visualizing it as a standard date and time. Add EventHandler CellFormating for the DataGridView.

private void dgvActiveUsers_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
{
  try
  {
    if (dgvActiveUsers.Columns[e.ColumnIndex].Name.Equals("logindate"))
    {
      DateTime origin = new DateTime(1970, 1, 1, 0, 0, 0, 0);
      e.Value = origin.AddSeconds(double.Parse(e.Value.ToString())).ToShortDateString() + " - " + origin.AddSeconds(double.Parse(e.Value.ToString())).ToShortTimeString();
     }
   }
   catch (Exception)
   {
   }
}

This method can give an idea and many other types of formatting values.


Unique visitors to post: 5

Categories: C# Tags: , ,
  1. No comments yet.
  1. No trackbacks yet.
Google Analytics integration offered by Wordpress Google Analytics Plugin