在 C# 中,使用 DataGridView 控件显示日期和时间数据时,可以通过以下方法格式化日期和时间单元格:
在 DataGridView 的单元格的 CellFormatting 事件中处理。在该事件的代码中,您可以检查单元格的数据类型,如果是日期时间类型,则更改其格式。
在 DataGridView 的列的 DefaultCellStyle 属性中处理。您可以设置该属性的 Format 字符串,以定义该列的默认日期时间格式。
以下是一个简单的例子,通过在 CellFormatting 事件中格式化单元格来显示日期和时间:
private void dataGridView1_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
if (e.ColumnIndex == yourDateTimeColIndex && e.Value != null)
e.Value = ((DateTime)e.Value).ToString("yyyy-MM-dd HH:mm:ss");
e.FormattingApplied = true;
KimYong