隐藏值00:00:00 TimeSpan在DataGridView的列中

时间:2018-05-03 09:19:11

标签: c# winforms datagridview timespan

我有DataGridView

DataTable
var dTable = new DataTable();
dTable.Columns.Add("Date", typeof(DateTime));
dTable.Columns.Add("Time", typeof(TimeSpan));

可能会发生TimeSpan,其值为 00:00:00

如果值为DateTime类型,是否有可能在MinValue中显示 00:00:00 值?

1 个答案:

答案 0 :(得分:2)

在DataGridView中使用CellFormatting事件,在不更改基础数据的情况下更改显示值(TimeSpan.Zero为“00:00:00”值)

private void Grid_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
{
    if (e.ColumnIndex < 0 || e.RowIndex < 0) return;
    var grid = (DataGridView)sender;

    if (grid.Columns[e.ColumnIndex].ValueType == typeof(TimeSpan) &&
        TimeSpan.Zero == (TimeSpan)grid[e.ColumnIndex, e.RowIndex].Value)
    {
        e.Value = " ";
        e.FormattingApplied = true;
    }
}