VB.NET XtraGrid中更改单元格颜色后,将其值修改 [英] VB.NET XtraGrid Change cell color after its value is edited

查看:412
本文介绍了VB.NET XtraGrid中更改单元格颜色后,将其值修改的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我怎样才能改变XtrsGrid的(GridControl)单元格背景后,它的价值已经被更新/变更/编辑?

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

编辑:我需要把每一个细胞的颜色变化,每当一个单元格的值更改

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

推荐答案

我终于成功地做到这一点,在下面的方式!

I finally managed to do it in the following way!

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

在它创建一个类和三个字段。

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

初​​始化列表中的 Form1_Load的的功能。

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

现在,在 GridView.CellValueChanged 事件,请执行以下操作:

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

现在,做 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上RowCellCustomDrawEventArgs 包含了所有必需的信息。我们只需要关心的编辑细胞指标,因为 GridView.CustomDrawCell 要求每次行/列的焦点发生改变。

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.

查看结果。

Before

And After

注意的黄色细胞有我改变了使用内联/就地编辑不同的值。

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

感谢

这篇关于VB.NET XtraGrid中更改单元格颜色后,将其值修改的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆