Show only Date in DataGridView Column

前端 未结 2 764
春和景丽
春和景丽 2021-01-27 05:46

I have a field as DateTime, I want to display only date( without time ) in my datagridview c#. I get the datetime. example: 22.03.2016 00:00:00 Any help would be appreciated. I

2条回答
  •  生来不讨喜
    2021-01-27 05:53

    The problem is your column is not of type DateTime so when you apply formatting, it doesn't work.

    Take a look at the method that creates the data table, you created the columns using dataTable.Columns.Add(prop.Name); which it means the DataType of column is string and formatting will not apply on column. (As I guessed previously)

    Note

    • You don't need to convert a List to a DataTable if you want to show it in DataGridView, it's enough to set it as DataSource of the grid.
    • To format a DateTime column to show only Date part, it's enough to set DefaultCellStyle.Format of that column to yyyy/MM/dd.
    • As a quick fix in your method, you can replace this line of code dataTable.Columns.Add(prop.Name); with this line of code: dataTable.Columns.Add(prop.Name, prop.PropertyType); which uses data type of property as data type of column.

提交回复
热议问题