Formatting DataGridView cell with custom DataSource
Posted on January 13, 2010
When you use a custom DataSource (in my case a generic list) the properties become the columns and the property names, become the column header.
I needed to format a DateTime field, so I did this:
1 2 3 4 5 6 7 8 9 | private void dataGridView1_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e) { if (dataGridView1.Columns[e.ColumnIndex].DataPropertyName == "Date") { DateTime value = (DateTime)e.Value; e.Value = value.ToString("G"); e.FormattingApplied = true; } } |
In this case, "Date" is the property name of a DateTime field.
In case you are curious, the DataSource assignment is like this:
1 2 3 | List<GoodReceipts> grrs = new List<GoodReceipts>(); grrs.AddRange(logic.GetGRRData(consumptions, validSince, validTo)); dataGridView1.DataSource = grrs; |