|Important||This document may not represent best practices for current development, links to downloads and other resources may no longer be valid. Current recommended version can be found here. ArchiveDisclaimer|
Data Formatting in the Windows Forms DataGridView Control
The DataGridView control provides automatic conversion between cell values and the data types that the parent columns display. Text box columns, for example, display string representations of date, time, number, and enumeration values, and convert user-entered string values to the types required by the data store.
The DataGridView control provides basic data formatting of cell values through the DataGridViewCellStyle class. You can use the Format property to format date, time, number, and enumeration values for the current default culture using the format specifiers described in Formatting Types. You can also format these values for specific cultures using the FormatProvider property. The specified format is used both to display data and to parse data that the user enters in the specified format.
The DataGridViewCellStyle class provides additional formatting properties for wordwrap, text alignment, and the custom display of null database values. For more information, see How to: Format Data in the Windows Forms DataGridView Control.
If the basic formatting does not meet your needs, you can provide custom data formatting in a handler for the DataGridView.CellFormatting event. The DataGridViewCellFormattingEventArgs passed to the handler has a Value property that initially contains the cell value. Normally, this value is automatically converted to the display type. To convert the value yourself, set the Value property to a value of the display type.
The CellFormatting event is also useful when you want to set DataGridViewCellStyle properties for individual cells based on their values. For more information, see How to: Customize Data Formatting in the Windows Forms DataGridView Control.