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

时间:2015-05-11 11:21:43

标签: .net vb.net datagridview devexpress

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

我可以在以下活动中执行此操作:

AddHandler grdView.RowCellStyle, AddressOf grdView_RowCellStyle

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

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

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

1 个答案:

答案 0 :(得分:1)

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

  1. 您需要处理两个事件:
    • GridView.CellValueChanged
    • GridView.CustomDrawCell
  2. 您需要跟踪每个已更改的单元格索引。所以,我们需要一个列表
  3. 在其中创建一个类和三个字段。

    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包含所有必需信息。我们只需要关注已编辑的单元格索引,因为每次更改行/列焦点时都会调用GridView.CustomDrawCell

    查看结果。

    <强>之前 enter image description here

    之后 enter image description here

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

    由于

相关问题