Show only Date in DataGridView Column

前端 未结 2 765
春和景丽
春和景丽 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.
    0 讨论(0)
  • 2021-01-27 05:56

    There is an easier way. Next time, if you add data to DataGridView via clicking the little triangle at the right-top corner of DataGridView;

    Edit your XXXDataSet.Designer according to:

    this.columnYYY = new global::System.Data.DataColumn("YYY", typeof(**string**), null, global::System.Data.MappingType.Element);

    Change String to DateTime. Problem solved enjoy. :)

    0 讨论(0)
提交回复
热议问题