Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Databound DataGridView Cell Format Int to Month Name Conversion

I have a databound datagridview. In it I have a column that has NULL values and 1-12. How can I at runtime make the datagridview turn the 1 into January and display it in the cell? And the NULL into another pre-defined string?

I tried looking at the cell format, changed it to custom, then put in "MMM" but it did not work and displayed "MMM" in the actual cell.

Please help.

EDIT: Its winforms c#4.0, the field is bound to Int field from an entity in EF. And no need for culture sensitive, english is fine.

EDIT: I tried {0:MMM} it didnt work, just displayed {X:MMM} in cell where X is int.

Some screenshots:

format

result

like image 710
sprocket12 Avatar asked Jan 23 '12 14:01

sprocket12


1 Answers

If WinForms try this:

Use Column.DefaultCellStyle.Format property or set it in designer.

This post should clarify any doubt:

Customize format of DateTime string in WPF and WinForm data binding


PROBLEM: string formatting masks will only work with DateTime values. You have int and in this case I think you'll have to write a converter function...

Check this:

DataGridView.CellFormatting Event

like image 81
Leniel Maccaferri Avatar answered Sep 22 '22 09:09

Leniel Maccaferri