Home
Search
 
What's New
Index
Books
Links
Q & A
Newsletter
Banners
 
Feedback
Tip Jar
 
C# Helper...
 
XML RSS Feed
Follow VBHelper on Twitter
 
 
 
MSDN Visual Basic Community
 
 
 
 
 
TitleDraw certain cells in a DataGrid with different colors in Visual Basic .NET
DescriptionThis example shows how to draw certain cells in a DataGrid with different colors in Visual Basic .NET.
KeywordsDataGrid, color, cell, colored cell, cutoff, VB.NET
CategoriesControls, Database, Software Engineering
 
The process starts by deriving a class from DataGridTextBoxColumn. This class overrides its inherited Paint event handler to set a cell's foreground and background colors. In this example, the code gives a cell a pink background if it does not contain a number greater than a cutoff value.
 
' A DataGridTextBoxColumn class that colors text box cells
' hot pink if their value is less than a given value.
Public Class ColoredTextBoxColumn
    Inherits DataGridTextBoxColumn

    Private m_Cutoff As Single = 0
    Public Sub New(ByVal cutoff As Single)
        m_Cutoff = cutoff
    End Sub

    Protected Overloads Overrides Sub Paint(ByVal g As _
        Graphics, ByVal bounds As Rectangle, ByVal source _
        As CurrencyManager, ByVal rowNum As Integer, ByVal _
        foreBrush As Brush, ByVal backBrush As Brush, ByVal _
        alignToRight As Boolean)
        ' Get the cell's value.
        Dim cell_object As Object
        cell_object = Me.GetColumnValueAtRow(source, rowNum)

        ' If the value is numeric, get its value.
        Dim cell_number As Single = m_Cutoff - 1
        If IsNumeric(cell_object) Then
            cell_number = CType(cell_object, Single)
        End If

        ' Pick appropriate colors.
        If (cell_number < m_Cutoff) Then
            backBrush = Brushes.Pink
            foreBrush = Brushes.Red
        Else
            backBrush = Brushes.White
            foreBrush = Brushes.Black
        End If

        ' Draw the cell.
        MyBase.Paint(g, bounds, source, rowNum, backBrush, _
            foreBrush, alignToRight)
    End Sub
End Class
 
The following code shows how the program uses this style class. The program makes a DataGridTableStyle object to manage the DataGrid's style. It creates a normal DataGridTextBoxColumn object to handle the table's first column: StudentName. It then creates a new ColoredTextBoxColumn object to manage the second column: CurrentGrade. The object's constructor sets the object's cutoff to 75.

The code adds these column objects to the table style object's GridColumnStyles collection and then adds the table style object to the DataGrid's TableStyles collection.

The code finishes by building some data in a DataTable and setting the DataGrid control's DataSource property to the DataTable.

 
Private Sub Form1_Load(ByVal sender As System.Object, ByVal _
    e As System.EventArgs) Handles MyBase.Load
    ' Use the colored column style.
    ' Make a TableStyle.
    Dim table_style As New DataGridTableStyle
    table_style.MappingName = "StudentScores"

    ' Make a DataGridTextBoxColumn for the name.
    Dim name_col As New DataGridTextBoxColumn
    name_col.MappingName = "StudentName"
    name_col.HeaderText = "Student Name"
    name_col.Width = 100

    ' Make a ColoredTextBoxColumn for the current grade.
    Dim score_col As New ColoredTextBoxColumn(75)
    score_col.MappingName = "CurrentGrade"
    score_col.HeaderText = "Current Grade"
    score_col.Width = 100

    ' Add the DataGridTextBoxColumns to the
     ' DataGridTableStyle.
    table_style.GridColumnStyles.Add(name_col)
    table_style.GridColumnStyles.Add(score_col)
    DataGrid1.TableStyles.Add(table_style)

    ' Make the data.
    Dim dt As New DataTable("StudentScores")
    dt.Columns.Add("StudentName")
    dt.Columns.Add("CurrentGrade")

    Dim rnd As New Random
    dt.Rows.Add(New Object() {"Andy Archer", rnd.Next(50, _
        100)})
    dt.Rows.Add(New Object() {"Betty Blaze", rnd.Next(50, _
        100)})
    dt.Rows.Add(New Object() {"Chas Chuckle", rnd.Next(50, _
        100)})
    dt.Rows.Add(New Object() {"Dee Diner", rnd.Next(50, _
        100)})
    dt.Rows.Add(New Object() {"Ed Eager", rnd.Next(50, _
        100)})
    dt.Rows.Add(New Object() {"Fran Fein", rnd.Next(50, _
        100)})
    dt.Rows.Add(New Object() {"Gus Gatlin", rnd.Next(50, _
        100)})
    dt.Rows.Add(New Object() {"Hellen Helpful", _
        rnd.Next(50, 100)})

    DataGrid1.DataSource = dt
End Sub
 
 
Copyright © 1997-2006 Rocky Mountain Computer Consulting, Inc.   All rights reserved.
  Updated