简体   繁体   中英

color datagridview cell and the one next to it based on condition

my code below only change color of 1 cell based on text value. How can I change color of a cell and the one on the next column based on same value. thanks

Private Sub DataGridView1_CellFormatting1(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellFormattingEventArgs) Handles DataGridView1.CellFormatting


     If Not DataGridView1.Rows(e.RowIndex).IsNewRow Then
        If e.ColumnIndex = DataGridView1.Columns("Dept1").Index Then
            If Not IsDBNull(e.Value) Then
                If e.Value = "Staring" Then
                    e.CellStyle.BackColor = Color.LightGray

                    e.CellStyle.Font = New Font("Arial", 16.0F)


                End If
            End If
        End If
    End If
End Sub

Did you try this?

DataGridView1.Rows(e.RowIndex).Cells(e.ColumnIndex + 1)

Something like:

If Not DataGridView1.Rows(e.RowIndex).IsNewRow Then
  If e.ColumnIndex = DataGridView1.Columns("Dept1").Index Then
    If Not IsDBNull(e.Value) Then
      If e.Value = "Staring" Then
        e.CellStyle.BackColor = Color.LightGray
        e.CellStyle.Font = New Font("Arial", 16.0F)

        Dim otherCell As DataGridViewCell =
          DataGridView1.Rows(e.RowIndex).Cells(e.ColumnIndex + 1)

        otherCell.Style.BackColor = e.CellStyle.BackColor
      End If
    End If
  End If
End If

References:

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM