Thursday, January 10, 2013

How to: Format Data in the Windows Forms DataGridView Control

.NET Framework 4.5


The following procedures demonstrate basic formatting of cell values using the DefaultCellStyle property of a DataGridView control and of specific columns in a control. For information about advanced data formatting, see How to: Customize Data Formatting in the Windows Forms DataGridView Control.

To format currency and date values

  • Set the Format property of a DataGridViewCellStyle. The following code example sets the format for specific columns using the DefaultCellStyle property of the columns. Values in the UnitPrice column appear in the current culture-specific currency format, with negative values surrounded by parentheses. Values in the ShipDate column appear in the current culture-specific short date format. For more information about Format values, see Formatting Types.
    this.dataGridView1.Columns["UnitPrice"].DefaultCellStyle.Format = "c";
    this.dataGridView1.Columns["ShipDate"].DefaultCellStyle.Format = "d";
    

To customize the display of null database values

To enable wordwrap in text-based cells

To specify the text alignment of DataGridView cells

private void SetFormatting()
{
    this.dataGridView1.Columns["UnitPrice"].DefaultCellStyle.Format = "c";
    this.dataGridView1.Columns["ShipDate"].DefaultCellStyle.Format = "d";
    this.dataGridView1.Columns["CustomerName"].DefaultCellStyle
        .Alignment = DataGridViewContentAlignment.MiddleRight;
    this.dataGridView1.DefaultCellStyle.NullValue = "no entry";
    this.dataGridView1.DefaultCellStyle.WrapMode =
        DataGridViewTriState.True;
}
These examples require:

No comments:

Post a Comment

Google Ads by Mavra

About Me

My photo
Jeddah, Makkah, Saudi Arabia
for more details www.inhousetoday.com