Results 1 to 4 of 4

Thread: DataGridView - Highlight Cell

  1. #1

    Thread Starter
    Fanatic Member The_Grudge's Avatar
    Join Date
    Jan 2005
    Location
    Canada
    Posts
    836

    DataGridView - Highlight Cell

    I have a datagridview in my project which is bound to a dataset.

    The grid populates fine, but I'm looking to do something a bit more.

    One of the columns is called "Status" and right now it returns the ID from the database in this column (e.g. "13", "14" etc). I'd like to substitute this value with it's associated color so if it's "13" set the cell background to green, if it's 14 set it to yellow and so on.

    Just not sure a) where to put this code and b) how to do it.

    Thanks

  2. #2
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Posts
    12,397

    Re: DataGridView - Highlight Cell

    Simply loop through your datagridview cells and if the cell value matches a certain number, then change the background color. I don't have VS installed on this computer, but it'd look like this:

    Code:
    For i As Integer = 0 to DataGridView1.Rows.Count - 1
            Dim row As DataGridViewRow = DataGridView1.Rows(i)
            Dim cell As DataGridViewCell = row.Cells(0)
    
            If cell.Value = "1" Then
                    'Do something if the ID is 1
            End If
                    
    Next
    "Code is like humor. When you have to explain it, it is bad." - Cory House
    VbLessons | HtmlLessons | CssLessons | Code Tags | Sword of Fury - Jameram

  3. #3
    PowerPoster dunfiddlin's Avatar
    Join Date
    Jun 2012
    Posts
    8,245

    Re: DataGridView - Highlight Cell

    vb.net Code:
    1. Private Sub DataGridView1_CellPainting(sender As System.Object, e As System.Windows.Forms.DataGridViewCellPaintingEventArgs) Handles DataGridView1.CellPainting
    2.         Dim c As Drawing.Color
    3.         For i = 0 To DataGridView1.Rows.Count - 1
    4.             Select Case CInt(DataGridView1.Rows(i).Cells(0).Value)
    5.                 Case 1
    6.                     c = Color.AliceBlue
    7.                 Case 2
    8.                     c = Color.Bisque
    9.                 Case 3
    10.                     c = Color.Coral
    11.                 Case Else
    12.                     c = Color.White
    13.             End Select
    14.             DataGridView1.Rows(i).Cells(0).Style.BackColor = c
    15.         Next
    16.     End Sub
    As the 6-dimensional mathematics professor said to the brain surgeon, "It ain't Rocket Science!"

    Reviews: "dunfiddlin likes his DataTables" - jmcilhinney

    Please be aware that whilst I will read private messages (one day!) I am unlikely to reply to anything that does not contain offers of cash, fame or marriage!

  4. #4
    Karen Payne MVP kareninstructor's Avatar
    Join Date
    Jun 2008
    Location
    Oregon
    Posts
    6,714

    Re: DataGridView - Highlight Cell

    Here is something to consider

    Code:
    Public Class Form1
        Private dictColors As New Dictionary(Of Int32, Color) From _
            {
                {1, Color.LightPink},
                {13, Color.LightGreen},
                {14, Color.Yellow}
            }
        Private Sub Form1_Load(
            ByVal sender As System.Object,
            ByVal e As System.EventArgs) Handles MyBase.Load
    
            Dim dt As New DataTable With {.TableName = "People"}
            dt.Columns.AddRange(New DataColumn() _
                {
                    New DataColumn("ID", GetType(Int32)),
                    New DataColumn("Name", GetType(String))
                }
            )
    
            dt.Rows.Add(New Object() {1, "AAA"})
            dt.Rows.Add(New Object() {13, "BBB"})
            dt.Rows.Add(New Object() {3, "CCC"})
            dt.Rows.Add(New Object() {14, "DDD"})
            dt.Rows.Add(New Object() {11, "EEE"})
    
            DataGridView1.DataSource = dt
    
        End Sub
        Private Sub DataGridView1_CellFormatting(
            ByVal sender As Object,
            ByVal e As System.Windows.Forms.DataGridViewCellFormattingEventArgs) _
        Handles DataGridView1.CellFormatting
    
            If Not DataGridView1.CurrentRow.IsNewRow Then
                If e.ColumnIndex = DataGridView1.Columns("ID").Index Then
                    If Not IsDBNull(e.Value) Then
                        If dictColors.ContainsKey(CInt(e.Value)) Then
                            e.CellStyle.BackColor = dictColors(CInt(e.Value))
                        End If
                    End If
                End If
            End If
        End Sub
    End Class

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width