Date and time the Datagridview was shown (VB.NET)

I have a datagridview where some columns are initially set as String and contain date or time depending on some business logic.

I am changing the column type (in datetime) to allow correct collation. And I would like to know if it is possible to show date values ​​or time values ​​depending on the string. Or I will always show both.



source to share

1 answer

You can handle the event CellFormatting

. This event is fired every time a cell is colored. You can change which value is displayed without changing the base value and cell format.

That being said, you can change the format depending on the row and column

Private Sub CellFormatting_EventHandler(sender As Object, e As DataGridViewCellFormattingEventArgs)
     If e.ColumnIndex = 2 AndAlso CType(sender, DataGridView)(5, e.RowIndex).Value.ToString = "Steve" Then 'Or whatever logic you like
         e.Value = CType(e.Value, Date).ToShortTimeString
     End If
 End Sub




All Articles