[英]color datagridview cell and the one next to it based on condition
我下面的代碼僅根據文本值更改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
你有嘗試過嗎?
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
參考文獻:
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.