Section 3.17. Format the DataGridView

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:


Sets how text is justified inside the cell.

BackColor and ForeColor

Set the color of the cell background and the color of the cell text.


Sets the font used for the cell text.


A 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 strings. For example, C designates a currency value. (For more information, look up the index entry "numeric format strings" in the MSDN help.)


A string of text that will be substituted for any null (missing) values.

SelectionBackColor and SelectionForeColor

Set the cell background colors and text colors for selected cells.


Determines if text will flow over multiple lines (if the row is high enough to accommodate it) or if it will be truncated. By default, cells will wrap.

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:

  1. DataGridViewCell.Style

  2. DataGridViewRow.DefaultCellStyle

  3. DataGridView.AlternatingRowsDefaultCellStyle

  4. DataGridView.RowsDefaultCellStyle

  5. DataGridViewColumn.DefaultCellStyle

  6. DataGridView.DefaultCellStyle

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.

Visual Basic 2005(c) A Developer's Notebook
Visual Basic 2005: A Developers Notebook
ISBN: 0596007264
EAN: 2147483647
Year: 2006
Pages: 123

Similar book on Amazon © 2008-2017.
If you may any questions please contact us: