I have a datagridview. The dgv’s data source is a ‘ReadOnlyCollection’ of objects. One of the properties of the object in the ‘ReadOnlyCollection’ is a nullable ‘DateTime’. What I’m trying to do is show a blank cell where the ‘DateTime’ is null, but apply a different format to the DateTime column, by doing:
TimeOutColumn.DefaultCellStyle.Format = "MM/dd/yyyy HH:mm";
The only problem is that sets all of my null entries to the DateTime.Min value.
‘history’ is the ReadOnlyCollection.
Here’s the current code:
'DataGridViewTextBoxColumn TimeOutColumn = new DataGridViewTextBoxColumn();
TimeOutColumn.DataPropertyName = "timeout";
TimeOutColumn.HeaderText = "Time Out";
ddgGHistory.Columns.Add(BadgeTypeIDColumn);
ddgGHistory.Columns.Add(EmployeeColumn);
ddgGHistory.Columns.Add(TimeInColumn);
ddgGHistory.Columns.Add(TimeOutColumn);
ddgGHistory.DataSource = history
ddgGHistory.Columns[3].DefaultCellStyle.Format = "MM/dd/yyyy HH:mm";
ddgGHistory.Columns[3].DefaultCellStyle.NullValue = "";
Since
DateTimeis a non-nullable object, it must have a value. However, it is possible to makeDateTimenullable by declaring it as a nullableDateTime.The following is a non-nullable datetime, and will always have a value.
However,one can declare it as nullable by adding
?after the type.Assuming that
nullable_dthas been set tonull, thedatagridviewwill show an empty cell where the value fromnullable_dthas been assigned.