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
 
 
 
 
 
TitleColor a DataGridView control's cells dynamically as their data changes in Visual Basic 2005
DescriptionThis example shows how to color a DataGridView control's cells dynamically as their data changes in Visual Basic 2005.
Keywordscolor cell, dynamic, DataGridView, VB 2005, database
CategoriesDatabase, Controls
 
This example builds a DataTable in memory and gives it some data. It then attaches the DataTable to the form's DataGridView control.
 
Private Sub Form1_Load(ByVal sender As System.Object, ByVal _
    e As System.EventArgs) Handles MyBase.Load
    ' Make a DataTable.
    Dim students_table As New DataTable("Students")

    ' Add columns.
    students_table.Columns.Add("FirstName", GetType(String))
    students_table.Columns.Add("LastName", GetType(String))
    students_table.Columns.Add("Quiz Average", _
        GetType(String))

    ' Make the combined FirstName/LastName unique.
    Dim first_last_columns() As DataColumn = { _
        students_table.Columns("FirstName"), _
        students_table.Columns("LastName") _
    }
    students_table.Constraints.Add( _
        New UniqueConstraint(first_last_columns))

    ' Make some contact data.
    students_table.Rows.Add(New Object() {"Art", "Ant", 75})
    students_table.Rows.Add(New Object() {"Bev", "Bug", 80})
    students_table.Rows.Add(New Object() {"Cid", "Cat", 97})
    students_table.Rows.Add(New Object() {"Deb", "Dove", _
        82})
    students_table.Rows.Add(New Object() {"Ed", "Eager", _
        67})
    students_table.Rows.Add(New Object() {"Fran", "Fix", _
        71})
    students_table.Rows.Add(New Object() {"Gus", "Gantry", _
        88})
    students_table.Rows.Add(New Object() {"Hil", "Harris", _
        63})

    ' Attach grdStudents to the DataTable.
    grdStudents.DataSource = students_table
End Sub
 
When the DataGridView control must display data, it raises its CellFormatting event. The following event handler catches the event. If the cell being formatted is a Quiz Average cell, then the code sets the cell's BackColor and ForeColor properties depending on the cell's current value.
 
' Make scores less than 75 pink.
Private Sub grdStudents_CellFormatting(ByVal sender As _
    Object, ByVal e As _
    System.Windows.Forms.DataGridViewCellFormattingEventArgs) _
    Handles grdStudents.CellFormatting
    If grdStudents.Columns(e.ColumnIndex).Name.Equals("Quiz " & _
        "Average") Then
        If CInt(e.Value) < 75 Then
            e.CellStyle.BackColor = Color.Pink
            e.CellStyle.ForeColor = Color.Red
        Else
            e.CellStyle.BackColor = _
                grdStudents.DefaultCellStyle.BackColor
            e.CellStyle.ForeColor = _
                grdStudents.DefaultCellStyle.ForeColor
        End If
    End If
End Sub
 
 
Copyright © 1997-2006 Rocky Mountain Computer Consulting, Inc.   All rights reserved.
  Updated