How to: Format Data in the Windows Forms DataGridView Control Page 1 of 4
©2010 Microsoft Corporation. All rights reserved.
.NET Framework 4 - Windows Forms
How to: Format Data in the Windows Forms DataGridView Control
The following procedures demonstrate basic formatting of cell values using the DefaultCellStyle
[ http://msdn.microsoft.com/en-us/library/system.windows.forms.datagridview.defaultcellstyle.aspx ] property of a
DataGridView [ http://msdn.microsoft.com/en-us/library/system.windows.forms.datagridview.aspx ] 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 [ http://msdn.microsoft.com/en-
us/library/z1cc356h.aspx ] .
To format currency and date values
Set the Format [ http://msdn.microsoft.com/en-
us/library/system.windows.forms.datagridviewcellstyle.format.aspx ] property of a DataGridViewCellStyle
[ http://msdn.microsoft.com/en-us/library/system.windows.forms.datagridviewcellstyle.aspx ] . The following
code example sets the format for specific columns using the DefaultCellStyle [ http://msdn.microsoft.com/en-
us/library/system.windows.forms.datagridviewcolumn.defaultcellstyle.aspx ] 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 [ http://msdn.microsoft.com/en-
us/library/system.windows.forms.datagridviewcellstyle.format.aspx ] values, see Formatting Types
[ http://msdn.microsoft.com/en-us/library/26etazsy.aspx ] .
Visual Basic Copy Code
Me.dataGridView1.Columns("UnitPrice").DefaultCellStyle.Format = "c"
Me.dataGridView1.Columns("ShipDate").DefaultCellStyle.Format = "d"
C# Copy Code
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 [ http://msdn.microsoft.com/en-
us/library/system.windows.forms.datagridviewcellstyle.nullvalue.aspx ] property of a DataGridViewCellStyle
[ http://msdn.microsoft.com/en-us/library/system.windows.forms.datagridviewcellstyle.aspx ] . The following
code example uses the DataGridView.DefaultCellStyle [ http://msdn.microsoft.com/en-
us/library/system.windows.forms.datagridview.defaultcellstyle.aspx ] property to display "no entry" in all cells
containing values equal to DBNull.Value [ http://msdn.microsoft.com/en-
us/library/system.dbnull.value.aspx ] .
Visual Basic Copy Code
Me.dataGridView1.DefaultCellStyle.NullValue = "no entry"
C# Copy Code
this.dataGridView1.DefaultCellStyle.NullValue = "no entry";
http://msdn.microsoft.com/en-us/library/f9x2790s(printer).aspx 9/6/2010
How to: Format Data in the Windows Forms DataGridView Control Page 2 of 4
To enable wordwrap in text-based cells
Set the WrapMode [ http://msdn.microsoft.com/en-
us/library/system.windows.forms.datagridviewcellstyle.wrapmode.aspx ] property of a DataGridViewCellStyle
[ http://msdn.microsoft.com/en-us/library/system.windows.forms.datagridviewcellstyle.aspx ] to one of the
DataGridViewTriState [ http://msdn.microsoft.com/en-
us/library/system.windows.forms.datagridviewtristate.aspx ] enumeration values. The following code example
uses the DataGridView.DefaultCellStyle [ http://msdn.microsoft.com/en-
us/library/system.windows.forms.datagridview.defaultcellstyle.aspx ] property to set the wrap mode for the
entire control.
Visual Basic Copy Code
Me.dataGridView1.DefaultCellStyle.WrapMode = DataGridViewTriState.True
C# Copy Code
this.dataGridView1.DefaultCellStyle.WrapMode =
DataGridViewTriState.True;
To specify the text alignment of DataGridView cells
Set the Alignment [ http://msdn.microsoft.com/en-
us/library/system.windows.forms.datagridviewcellstyle.alignment.aspx ] property of a DataGridViewCellStyle
[ http://msdn.microsoft.com/en-us/library/system.windows.forms.datagridviewcellstyle.aspx ] to one of the
DataGridViewContentAlignment [ http://msdn.microsoft.com/en-
us/library/system.windows.forms.datagridviewcontentalignment.aspx ] enumeration values. The following
code example sets the alignment for a specific column using the DefaultCellStyle
[ http://msdn.microsoft.com/en-us/library/system.windows.forms.datagridviewcolumn.defaultcellstyle.aspx ]
property of the column.
Visual Basic Copy Code
Me.dataGridView1.Columns("CustomerName").DefaultCellStyle _
.Alignment = DataGridViewContentAlignment.MiddleRight
C# Copy Code
this.dataGridView1.Columns["CustomerName"].DefaultCellStyle
.Alignment = DataGridViewContentAlignment.MiddleRight;
Example
Visual Basic Copy Code
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
http://msdn.microsoft.com/en-us/library/f9x2790s(printer).aspx 9/6/2010
How to: Format Data in the Windows Forms DataGridView Control Page 3 of 4
C# Copy Code
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 [ http://msdn.microsoft.com/en-us/library/system.windows.forms.datagridview.aspx ]
control named dataGridView1 that contains a column named UnitPrice, a column named ShipDate, and
a column named CustomerName.
References to the System [ http://msdn.microsoft.com/en-us/library/system.aspx ] , System.Drawing
[ http://msdn.microsoft.com/en-us/library/system.drawing.aspx ] , and System.Windows.Forms
[ http://msdn.microsoft.com/en-us/library/system.windows.forms.aspx ] assemblies.
Robust Programming
For maximum scalability, you should share DataGridViewCellStyle [ http://msdn.microsoft.com/en-
us/library/system.windows.forms.datagridviewcellstyle.aspx ] 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 [ http://msdn.microsoft.com/en-
us/library/ha5xt0d9.aspx ] .
See Also
Tasks
How to: Customize Data Formatting in the Windows Forms DataGridView Control
[ http://msdn.microsoft.com/en-us/library/z1cc356h.aspx ]
Reference
DataGridView.DefaultCellStyle [ http://msdn.microsoft.com/en-
us/library/system.windows.forms.datagridview.defaultcellstyle.aspx ]
DataGridViewBand.DefaultCellStyle [ http://msdn.microsoft.com/en-
us/library/system.windows.forms.datagridviewband.defaultcellstyle.aspx ]
DataGridViewCellStyle [ http://msdn.microsoft.com/en-
us/library/system.windows.forms.datagridviewcellstyle.aspx ]
Concepts
Cell Styles in the Windows Forms DataGridView Control [ http://msdn.microsoft.com/en-
us/library/1yef90x0.aspx ]
Data Formatting in the Windows Forms DataGridView Control [ http://msdn.microsoft.com/en-
us/library/hezscd0d.aspx ]
Other Resources
Basic Formatting and Styling in the Windows Forms DataGridView Control [ http://msdn.microsoft.com/en-
us/library/ms171598.aspx ]
Formatting Types [ http://msdn.microsoft.com/en-us/library/26etazsy.aspx ]
Tags:
Community Content
Simple DatagridView Samples Last Edit 8:28 AM by suzett1
http://msdn.microsoft.com/en-us/library/f9x2790s(printer).aspx 9/6/2010
How to: Format Data in the Windows Forms DataGridView Control Page 4 of 4
<br /> http://vb.net-informations.com/datagridview/vb.net_datagridview_tutorial.htm<br /><br /> Suzett.
Tags:
http://msdn.microsoft.com/en-us/library/f9x2790s(printer).aspx 9/6/2010