VB.NET XtraGrid 在编辑其值后更改单元格颜色

VB.NET XtraGrid Change cell color after its value is edited

XtrsGrid (GridControl) 的单元格背景值已 updated/changed/edited 后如何更改?

我可以在以下事件中这样做吗:

AddHandler grdView.RowCellStyle, AddressOf grdView_RowCellStyle

但这会改变整个网格单元格的颜色。

Private Sub grdView_RowCellStyle(sender As Object, e As RowCellStyleEventArgs)
    e.Appearance.BackColor = Color.Blue
End Sub

编辑:每当更改单元格值时,我都需要改变每个单元格的颜色。

我终于通过以下方式做到了!

  1. 您需要处理两个事件:
    • GridView.CellValueChanged
    • GridView.CustomDrawCell
  2. 您需要跟踪每个更改的单元格的索引。所以,我们需要一个 List

创建一个 class 和其中的三个字段。

Public Class UpdatedCell 
  'UC means UpdatedCll
  Public Property UCFocusedRow As Integer
  Public Property UCFocusedColumnIndex As Integer
  Public Property UCFieldName As String

  Public Sub New()
    UCFocusedRow = -1
    UCFocusedColumnIndex = -1
    UCFieldName = String.Empty
  End Sub

End Class

在您的 Form1_Load 函数中初始化列表。

Public lst As List(Of UpdatedCell) = New List(Of UpdatedCell)()

现在,在 GridView.CellValueChanged 事件中,执行以下操作:

Private Sub grdView_CellValueChanged(sender As Object, e As DevExpress.XtraGrid.Views.Base.CellValueChangedEventArgs)

    Dim currCell As New UpdatedCell
    currCell.UCFocusedRow = e.RowHandle
    currCell.UCFocusedColumnIndex = e.Column.AbsoluteIndex
    currCell.UCFieldName = e.Column.FieldName

    lst.Add(currCell)

End Sub

现在,在 GridView.CustomDrawCell 事件中执行以下操作:

Private Sub grdView_CustomDrawCell(sender As Object, e As RowCellCustomDrawEventArgs)

    Dim prevColor As Color = e.Appearance.BackColor

    For Each c As UpdatedCell In lst
        If e.RowHandle = c.UCFocusedRow And
        e.Column.AbsoluteIndex = c.UCFocusedColumnIndex And
        e.Column.FieldName = c.UCFieldName Then

            e.Appearance.BackColor = Color.Yellow

        Else
            If Not e.Appearance.BackColor = Color.Yellow Then
                e.Appearance.BackColor = prevColor
            End If

        End If
    Next

End Sub

请注意,参数 e As RowCellCustomDrawEventArgs 包含所有必需的信息。我们只需要关心编辑的单元格索引,因为每次 row/column 焦点改变时 GridView.CustomDrawCell 调用。

查看结果。

之前

之后

注意 黄色单元格具有我使用 inline/inplace 编辑器更改的不同值。

谢谢