【问题标题】:VB.NET XtraGrid Change cell color after its value is editedVB.NET XtraGrid 在其值被编辑后更改单元格颜色
【发布时间】:2015-05-11 11:21:43
【问题描述】:

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

我可以在以下活动中这样做吗:

AddHandler grdView.RowCellStyle, AddressOf grdView_RowCellStyle

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

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

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

【问题讨论】:

    标签: .net vb.net datagridview devexpress


    【解决方案1】:

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

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

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

    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

    查看结果。

    之前

    之后

    注意黄色单元格具有不同的值,我使用内联/就地编辑器更改了这些值。

    谢谢

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-12-05
      • 2021-10-20
      • 2021-07-01
      • 2021-11-04
      • 2019-12-13
      • 2014-08-24
      • 2020-09-25
      • 1970-01-01
      相关资源
      最近更新 更多