在 DataGridView WinForm 中设置空 null DateTime

本文关键字:null DateTime 设置 DataGridView WinForm | 更新日期: 2023-09-27 18:34:16

如何在DatagridView中格式化空日期时间?我以编程方式创建数据网格视图列,以加载的形式

DataGridViewCellStyle style = new DataGridViewCellStyle();
style.Format = "dd-MM-yyyy";
DataGridViewTextBoxColumn updatedDateColumn = new DataGridViewTextBoxColumn();
updatedDateColumn.DataPropertyName = "UpdatedDate";
updatedDateColumn.HeaderText = "Updated Date";
updatedDateColumn.Name = "UpdatedDate";
updatedDateColumn.DefaultCellStyle = style;

问题是当更新日期值为空时数据网格视图显示 01/01/0001 12:00:00

我尝试编辑

style.NullValue = string.Empty;

但它没有用如何解决这个问题?谢谢

在 DataGridView WinForm 中设置空 null DateTime

您可以使用 ISNULL 在 sql server 中将 null date 设置为 null,并将 datagridviewCellStyle 的 NullValue 属性设置为字符串。空;就像SQL Server中的这个一样

ISNULL(DateColumnName , NULL) and set style.NullValue = string.Empty   

in dataGridViewCellStyle