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

隐身守侯 提交于 2019-12-20 05:46:10

问题


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

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:


I finally managed to do it in the following way!

  1. you need to handle two events:
    • GridView.CellValueChanged
    • GridView.CustomDrawCell
  2. You need to keep track of every changed cell's indices. So, we need a 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.

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

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

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:

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.

See the result.

Before

And After

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

Thanks



来源:https://stackoverflow.com/questions/30166372/vb-net-xtragrid-change-cell-color-after-its-value-is-edited

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!