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

2024-01-05

在更新/更改/编辑 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

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


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

  1. you need to handle two events:
    • GridView.CellValueChanged
    • GridView.CustomDrawCell
  2. 您需要跟踪每个更改的单元格的索引。所以,我们需要一个List

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

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)()

Now, in 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 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

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

查看结果。

Before enter image description here

And After enter image description here

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

Thanks

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

VB.NET XtraGrid 在编辑其值后更改单元格颜色 的相关文章

随机推荐