3.17. 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.
3.17.1. 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:
The interesting part is that you can create and set DataGridViewCellStyle objects at different levels. When the DataGridView displays a cell, it looks for style information in several places. Here's the order from highest to lowest importance:
In other words, if DataGridView finds a DataGridViewCellStyle object assigned to the current cell (option 1), it always uses it. If not, it checks the DataGridViewCellStyle for the row, and so on.
The following code snippet performs column-specific formatting. It ensures that all the values in the CustomerID column are given a different font, alignment, and set of colors. Figure 3-13 shows the result.
Note: If you use the design-time data-binding features of Visual Studio, you can avoid writing this code altogether. Just click the Edit Columns link in the Properties Window and use the designer to choose the formatting.
Dim Style As DataGridViewCellStyle = _ DataGridView1.Columns("CustomerID").DefaultCellStyle Style.Font = New Font(DataGridView1.Font, FontStyle.Bold) Style.Alignment = DataGridViewContentAlignment.MiddleRight Style.BackColor = Color.LightYellow Style.ForeColor = Color.DarkRed
Figure 3-13. A DataGridView with a formatted column
3.17.2. What about...
...the easiest way to apply custom cell formatting? Sometimes, you want to call attention to cells with certain values. You could handle this task by iterating over the entire grid, looking for those cells that interest you. However, you can save time by responding to the DataGridView.CellFormatting event. This event occurs as the grid is being filled. It gives you the chance to inspect the cell and change its style before it appears.
Here's an example that formats a cell to highlight high prices:
Private Sub DataGridView1_CellFormatting(ByVal sender As System.Object, _ ByVal e As System.Windows.Forms.DataGridViewCellFormattingEventArgs) _ Handles DataGridView1.CellFormatting ' Check if this is the right column. If DataGridView1.Columns(e.ColumnIndex).Name = "Price" Then ' Check if this is the right value. If e.Value > 100 Then e.CellStyle.ForeColor = Color.Red e.CellStyle.BackColor = Color.Yellow End If End If End Sub
Keep in mind that you should reuse style objects if at all possible. If you assign a new style object to each cell, you'll consume a vast amount of memory. A better approach is to create one style object, and assign it to multiple cells that use the same formatting.