✅ 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:
public partial class EmployeeManagementForm : Form
{
private SmiledbContext dbContext;

public EmployeeManagementForm()
{
InitializeComponent();
this.dbContext = new SmiledbContext();
}

protected override void OnLoad(EventArgs e)
{
base.OnLoad(e);
//SmiledbContext context = new SmiledbContext();

this.dbContext.Departments.Load();
this.dbContext.Employees.Load();

this.departmentBindingSource.DataSource = this.dbContext.Departments.Local.ToBindingList();
}

private void departmentDataGridView_SizeChanged(object sender, EventArgs e)
{
SmiledbContext context = new SmiledbContext();
Department? dept = (Department)this.departmentDataGridView.CurrentRow.DataBoundItem;

if (dept != null)
{
context.Entry(dept).Collection(e => e.Employees).Load();
}
}
}
public partial class EmployeeManagementForm : Form
{
private SmiledbContext dbContext;

public EmployeeManagementForm()
{
InitializeComponent();
this.dbContext = new SmiledbContext();
}

protected override void OnLoad(EventArgs e)
{
base.OnLoad(e);
//SmiledbContext context = new SmiledbContext();

this.dbContext.Departments.Load();
this.dbContext.Employees.Load();

this.departmentBindingSource.DataSource = this.dbContext.Departments.Local.ToBindingList();
}

private void departmentDataGridView_SizeChanged(object sender, EventArgs e)
{
SmiledbContext context = new SmiledbContext();
Department? dept = (Department)this.departmentDataGridView.CurrentRow.DataBoundItem;

if (dept != null)
{
context.Entry(dept).Collection(e => e.Employees).Load();
}
}
}
10 Replies
OlujA
OlujA3d ago
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
Dev_Persi
Dev_Persi3d ago
You can ask the database to give you a human readable format (depending on the database) Big assumption but if you are using an MS product then look here
enrico11011
enrico11011OP3d ago
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
OlujA
OlujA3d ago
yup. DTO = Data Transfer Object
public class DepartmentDto
{
public int Id { get; set; }
public string Name { get; set; }
public long DateUnix { get; set; }

public string DateFormatted =>
DateTimeOffset.FromUnixTimeSeconds(DateUnix)
.LocalDateTime
.ToString("yyyy-MM-dd HH:mm:ss");
}
public class DepartmentDto
{
public int Id { get; set; }
public string Name { get; set; }
public long DateUnix { get; set; }

public string DateFormatted =>
DateTimeOffset.FromUnixTimeSeconds(DateUnix)
.LocalDateTime
.ToString("yyyy-MM-dd HH:mm:ss");
}
This is an example for Dto, you should use DateFormatted in your datagridview also
.ToString("dd-MM-yyyy");
.ToString("dd-MM-yyyy");
is 03-05-2025, you can format it like however you want it
enrico11011
enrico11011OP3d ago
would I have to rewrite my stuff so that I"m filling the datagridview manually via .Rows.Add()? i'm guessing 'yes'
Dev_Persi
Dev_Persi3d ago
I found a stackoverflow answer that suggested handling the columnadded event and checking against the name of the column
OlujA
OlujA3d ago
ah no, your OnLoad method should be like this
protected override void OnLoad(EventArgs e)
{
base.OnLoad(e);

this.dbContext.Departments.Load();

var departmentDtos = this.dbContext.Departments
.Local
.Select(d => new DepartmentDto
{
Id = d.Id,
Name = d.Name,
Date = d.DateFormatted
})
.ToList();

this.departmentBindingSource.DataSource = departmentDtos;
this.departmentDataGridView.DataSource = this.departmentBindingSource;
}
protected override void OnLoad(EventArgs e)
{
base.OnLoad(e);

this.dbContext.Departments.Load();

var departmentDtos = this.dbContext.Departments
.Local
.Select(d => new DepartmentDto
{
Id = d.Id,
Name = d.Name,
Date = d.DateFormatted
})
.ToList();

this.departmentBindingSource.DataSource = departmentDtos;
this.departmentDataGridView.DataSource = this.departmentBindingSource;
}
enrico11011
enrico11011OP3d ago
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 function
OlujA
OlujA2d ago
idk 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
public string DateFormatted
{
get
{
return DateTimeOffset.FromUnixTimeSeconds(DateUnix).ToString("yyyy-MM-dd");
}
}
public string DateFormatted
{
get
{
return DateTimeOffset.FromUnixTimeSeconds(DateUnix).ToString("yyyy-MM-dd");
}
}
enrico11011
enrico11011OP2d ago
ahh ok okok i think i get it now, thanks

Did you find this page helpful?