简体   繁体   中英

DeafultcCellStyle format not working on unbound DataGridView C# Winforms

I have an unbound DataGridView that I populate with data from an SQL database. One of the columns in the database is of type 'datetime'.

On the load event of my winforms application, I use code below in order to format the column to only show date part of datetime column. However it shows the full datetime. I want 18/02/2020 and it's showing 18/02/2020 00:00:00

dgv.Columns["Date"].DefaultCellStyle.Format = "dd/MM/yyyy"
dgv.Columns["Date"].ValueType = typeof(System.DateTime);

How do I format the column to only show the date portion of the datetime column?

I added some data in the gridview within my code and tried your code above. The DefaultCellStyle.Format works well.

Which means that you might need to check the way you're retrieving the data from your SQL Database. You may be filling your grid with strings instead of "dates" for example.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM