Set DataFormatString on DataGridView at Runtime?

macleojw picture macleojw · Aug 13, 2009 · Viewed 9.2k times · Source

Is it possible to set the DataFormatString property of a column or cell in an ASP.NET DataGridView at runtime?

Answer

JamieGaines picture JamieGaines · Aug 13, 2009

This should work.

BoundField priceField = grid.Columns[0] as BoundField;
priceField.DataFormatString = "{0:c}";
priceField.HtmlEncode = false;
grid.DataSource = list;
grid.DataBind();

Found via http://geekswithblogs.net/michelotti/archive/2006/03/30/73896.aspx