color datagridview单元格和基于条件的旁边单元格

时间:2014-05-29 15:02:54

标签: vb.net

下面的代码只根据文本值更改1个单元格的颜色。如何根据相同的值更改单元格的颜色和下一列的颜色。感谢

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

1 个答案:

答案 0 :(得分:0)

你试过这个吗?

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

类似的东西:

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

参考文献:

相关问题