Saturday, February 19, 2011

Format Data in the Windows Forms DataGridView Control

Useful coding for formation the cells in datagridview in MSDN site

To enable wordwrap in text-based cells

this.dataGridView1.DefaultCellStyle.WrapMode =     DataGridViewTriState.True;

To specify the text alignment of DataGridView cells

this.dataGridView1.Columns["CustomerName"].DefaultCellStyle     .Alignment = DataGridViewContentAlignment.MiddleRight;

To customize the display of null database values

this.dataGridView1.DefaultCellStyle.NullValue = "no entry";

To format currency and date values

this.dataGridView1.Columns["UnitPrice"].DefaultCellStyle.Format = "c"; this.dataGridView1.Columns["ShipDate"].DefaultCellStyle.Format = "d";

And also as below

dataGridView1.Columns[0].DefaultCellStyle.Format = "dd'/'MM'/'yyyy";

//Get 13/12/2007

dataGridView1.Columns[1].DefaultCellStyle.Format = "dd'/'MM'/'yyyy hh:mm:ss tt";

//Get 13/12/2007 5:00:11 PM

No comments:

Post a Comment