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.
- Alignment
- BackColor and ForeColor
- Font
- Format
- 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.)
- NullText
- SelectionBackColor and SelectionForeColor
- WrapMode
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:
DataGridViewCell.Style
DataGridViewRow.DefaultCellStyle
DataGridView.AlternatingRowsDefaultCellStyle
DataGridView.RowsDefaultCellStyle
DataGridViewColumn.DefaultCellStyle
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
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
No comments:
Post a Comment