Format the DataGridView
Formatting the .NET 1.x DataGrid ranges from awkward to nearly
impossible. However, thanks to its multi-layered model, formatting the
DataGridView is far easier. This
model builds on a single class, the DataGridViewCellStyle, which encapsulates key
formatting properties. You can assign different DataGridViewCellStyle objects to separate
rows, columns, or even distinct cells.
Note
By using a few simple style properties, you can configure the appearance of the entire grid, individual columns, or rows with important data.
How do I do that?
The DataGridView already
looks better than the DataGrid in
its default state. For example, you'll notice that the column headers
have a modern, flat look and become highlighted when the user moves
the mouse over them. However, there's much more you can do with the
help of the DataGridViewCellStyle
class.
The DataGridViewCellStyle
collects all the formatting properties of the DataGridView. It defines appearance-related
settings (e.g., color, font), and data formatting (e.g., currency,
date formats). All in all, the DataGridViewCellStyle provides the following
key properties:
AlignmentSets how text is justified inside the cell.
BackColorandForeColorSet the color of the cell background and the color of the cell text.
FontSets the font used for the cell text.
FormatA format string that configures how numeric or date data values will be formatted as strings. You can use the standard .NET format specifiers and your own custom format ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access