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

时间:2022-08-28 14:45:42

How can I change the XtrsGrid's (GridControl) cell background after its value has been updated/changed/edited?

如何在更新/更改/编辑其值后更改XtrsGrid(GridControl)单元格背景?

Can I do it in following event:

我可以在以下活动中完成:

AddHandler grdView.RowCellStyle, AddressOf grdView_RowCellStyle

But this changes the color of whole Grid cells.

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

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

EDIT: I need to turn every cell color change whenever a cell value is changed.

编辑:每当细胞值改变时,我需要改变每个细胞的颜色变化。

1 个解决方案

#1


I finally managed to do it in the following way!

我终于设法以下面的方式做到了!

  1. you need to handle two events:
    • GridView.CellValueChanged
    • GridView.CustomDrawCell
  2. 你需要处理两个事件:GridView.CellValueChanged GridView.CustomDrawCell

  3. You need to keep track of every changed cell's indices. So, we need a List
  4. 您需要跟踪每个已更改的单元格的索引。所以,我们需要一个List

Create a class and three fields in it.

在其中创建一个类和三个字段。

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

Initialize the list in your Form1_Load function.

初始化Form1_Load函数中的列表。

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

Now, in GridView.CellValueChanged event, do the following:

现在,在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

Now, do the following in GridView.CustomDrawCell event:

现在,在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

Note that the argument e As RowCellCustomDrawEventArgs contains all required information. We just need to care of edited cells indices because GridView.CustomDrawCell calls every time row/column focus is changed.

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

See the result.

看到结果。

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

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

NOTE that yellow cells have different values that I changed using inline/inplace editor.

请注意,黄色单元格具有不同的值,我使用内联/内置编辑器进行了更改。

Thanks

#1


I finally managed to do it in the following way!

我终于设法以下面的方式做到了!

  1. you need to handle two events:
    • GridView.CellValueChanged
    • GridView.CustomDrawCell
  2. 你需要处理两个事件:GridView.CellValueChanged GridView.CustomDrawCell

  3. You need to keep track of every changed cell's indices. So, we need a List
  4. 您需要跟踪每个已更改的单元格的索引。所以,我们需要一个List

Create a class and three fields in it.

在其中创建一个类和三个字段。

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

Initialize the list in your Form1_Load function.

初始化Form1_Load函数中的列表。

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

Now, in GridView.CellValueChanged event, do the following:

现在,在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

Now, do the following in GridView.CustomDrawCell event:

现在,在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

Note that the argument e As RowCellCustomDrawEventArgs contains all required information. We just need to care of edited cells indices because GridView.CustomDrawCell calls every time row/column focus is changed.

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

See the result.

看到结果。

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

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

NOTE that yellow cells have different values that I changed using inline/inplace editor.

请注意,黄色单元格具有不同的值,我使用内联/内置编辑器进行了更改。

Thanks