✅ converting / casting data before its displayed in a DataGridView
So I have a datagridview that I've got hooked up to my db, and one of the columns on the table it's displaying is a Unix timestamp. I'd like to convert this to human readable date before it's displayed in the DataGridView is there any way to do that? here's the snippet that handles the binding:
10 Replies
You can use CellFormatting method forDataGridView, but since you want it before sending it to DataGridView and you are using EF, i suggest have DTO or view model and format it there before
DTO? not familiar with that term. I'm assuming I need some kind of class sitting between EF and Winforms that'll handle the conversion
yup.
DTO = Data Transfer Object
This is an example for Dto, you should use DateFormatted in your datagridview
also is 03-05-2025, you can format it like however you want it
would I have to rewrite my stuff so that I"m filling the datagridview manually via .Rows.Add()? i'm guessing 'yes'
I found a stackoverflow answer that suggested handling the columnadded event and checking against the name of the column
ah no, your OnLoad method should be like this
ok i think i get it - question about the
Date = d.DateFormatted
line, is the omission of ()
intentional there? coming from a Python background this would assign a function object to the Date
attribute and not call the functionidk much about python but in c# that does not assign a function object, it actually calls the getter of the DateFormatted property. So it's actually this
ahh ok
okok i think i get it now, thanks