locked
Change cell background color programatically in a datagridview

    Question

  • I have a datagrid view with 8 columns.  How do I change the background color for any cell in columns 2-5 where the value equals the value in column 8.

    For instance:

    col1       col2                 3 4   5    6     7    8
    2116 7/14/2007 3 5 18 33 35 1
    2112 6/30/2007 1 2 15 30 37 1

    in row 2 I want the cell# 3 where the 1 appears to have a background color of red

    I need to do this in a loop through the whole dataset being displayed.

    So far I've gotten this far...but now I'm stuck at the 'TODO

     

    Private Sub HighlightMega()

     

    For Each row As DataGridViewRow In Vw_DrawingsDataGridView.Rows

     

    Dim cell As DataGridViewCell = row.Cells(7)

     

    Dim megaNumber As Integer = cell.Value

     

    For i = 2 To 6 ' Examine contents of cells 2 through 6 for a match with cell 7

     

    Dim cellNumber As DataGridViewCell = row.Cells(i)

     

    Dim number As Integer = cellNumber.Value

     

    If number = megaNumber Then

     

    'TODO Code to set cell background color of the number that matches the mega

     

    End If

     

    Next

     

    Next

     

    End Sub




     
    Tom Currier
    Wednesday, March 03, 2010 2:26 AM

Answers

  • Hi TheOneTopCat,

    You can change the cell color like this:

    row.Cells(i).Style.BackColor = Color.Red


    Best regards,
    Alex Liang

     
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
    • Marked as answer by TheOneTopCat Thursday, March 04, 2010 4:06 PM
    Thursday, March 04, 2010 6:59 AM
    Moderator

All replies

  • Oops, the columns reformatted when I submitted this.  However, the idea should be explained by the code I've written so far.
    Tom Currier
    Wednesday, March 03, 2010 2:27 AM
  • From the below, Datatable-Dt’s First Column and Second column matched Datas will be in Light Blue color.

    Dim dt As New System.Data.DataTable, AStyle As New DataGridViewCellStyle

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

    dt.Columns.Add("First_Column", Type.GetType("System.String"))
    dt.Columns.Add(
    "Second_Column", Type.GetType("System.String"))
    If Trim(TextBox1.Text) =
    "" And Trim(TextBox2.Text) = "" Then
    dt.Rows.Add(
    "ONE", "ON")
    dt.Rows.Add(
    "ONE", "ONE")
    dt.Rows.Add(
    "ONE", "OE")
    dt.Rows.Add(
    "TWO", "TW")
    dt.Rows.Add(
    "TWO", "TWO")
    dt.Rows.Add(
    "TWO", "TO")
    End If

    DataGridView1.DataSource = dt
    AStyle.BackColor = Color.LightBlue

    For i As Integer = 0 To dt.Rows.Count - 1
    If dt.Rows(i).Item(
    "First_Column").ToString =dt.Rows(i).Item("Second_Column").ToString Then
    DataGridView1.Rows(i).Cells(
    "First_Column").Style = AStyle
    DataGridView1.Rows(i).Cells(
    "Second_Column").Style = AStyle
    End If
    Next

    End Sub

    And the same condition can be applied to DataGridView_CellEndEdit() event….like the below code for further any new rows will be added

    Private Sub DataGridView1_CellEndEdit(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.CellEndEdit

            If e.ColumnIndex = 1 Then

                If DataGridView1.CurrentRow.Cells(0).Value.ToString = DataGridView1.CurrentRow.Cells(1).Value.ToString Then

                    DataGridView1.CurrentRow.Cells(0).Style = AStyle

                    DataGridView1.CurrentRow.Cells(1).Style = AStyle

                End If

            End If

        End Sub

     


    U.PARANTHAMAN
    Thursday, March 04, 2010 6:54 AM
  • Hi TheOneTopCat,

    You can change the cell color like this:

    row.Cells(i).Style.BackColor = Color.Red


    Best regards,
    Alex Liang

     
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
    • Marked as answer by TheOneTopCat Thursday, March 04, 2010 4:06 PM
    Thursday, March 04, 2010 6:59 AM
    Moderator
  • Thank you!!! I knew this would be easy, just couldn't find it!!!!
    Tom Currier
    Thursday, March 04, 2010 4:07 PM