How to: Format Data in the Windows Forms DataGridView Control

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 theUnitPricecolumn appear in the current culture-specific currency format, with negative values surrounded by parentheses. Values in theShipDatecolumn appear in the current culture-specific short date format. For more information about Format values, see Formatting Types.

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

To customize the display of null database values

  • Set the NullValue property of a DataGridViewCellStyle. The following code example uses the System.Windows.Forms.DataGridView.DefaultCellStyle property to display "no entry" in all cells containing values equal to System.DBNull.Value.

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

To enable wordwrap in text-based cells

  • Set the WrapMode property of a DataGridViewCellStyle to one of the DataGridViewTriState enumeration values. The following code example uses the System.Windows.Forms.DataGridView.DefaultCellStyle property to set the wrap mode for the entire control.

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

To specify the text alignment of DataGridView cells

  • Set the Alignment property of a DataGridViewCellStyle to one of the DataGridViewContentAlignment enumeration values. The following code example sets the alignment for a specific column using the DefaultCellStyle property of the column.

    Me.dataGridView1.Columns("CustomerName").DefaultCellStyle _
        .Alignment = DataGridViewContentAlignment.MiddleRight
    
    this.dataGridView1.Columns["CustomerName"].DefaultCellStyle
        .Alignment = DataGridViewContentAlignment.MiddleRight;
    

Example

Private Sub SetFormatting()
    With Me.dataGridView1
        .Columns("UnitPrice").DefaultCellStyle.Format = "c"
        .Columns("ShipDate").DefaultCellStyle.Format = "d"
        .Columns("CustomerName").DefaultCellStyle.Alignment = _
            DataGridViewContentAlignment.MiddleRight
        .DefaultCellStyle.NullValue = "no entry"
        .DefaultCellStyle.WrapMode = DataGridViewTriState.True
    End With
End Sub
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;
}

Compiling the Code

These examples require:

  • A DataGridView control named dataGridView1 that contains a column named UnitPrice, a column named ShipDate, and a column named CustomerName.

  • References to the System, System.Drawing, and System.Windows.Forms assemblies.

Robust Programming

For maximum scalability, you should share DataGridViewCellStyle objects across multiple rows, columns, or cells that use the same styles rather than setting the style properties for each element separately. For more information, see Best Practices for Scaling the Windows Forms DataGridView Control.

See Also

Tasks

How to: Customize Data Formatting in the Windows Forms DataGridView Control

Reference

System.Windows.Forms.DataGridView.DefaultCellStyle
System.Windows.Forms.DataGridViewBand.DefaultCellStyle
DataGridViewCellStyle

Concepts

Cell Styles in the Windows Forms DataGridView Control
Data Formatting in the Windows Forms DataGridView Control

Other Resources

Basic Formatting and Styling in the Windows Forms DataGridView Control
Formatting Types