VB.NET XtraGrid修改其值后更改单元格颜色 [英] VB.NET XtraGrid Change cell color after its value is edited
问题描述
我可以在以下事件中执行以下操作:
p> AddHandler grdView.RowCellStyle,AddressOf grdView_RowCellStyle
但是这会改变整个网格单元格的颜色。
Private Sub grdView_RowCellStyle(sender As Object, e As RowCellStyleEventArgs)
e.Appearance.BackColor = Color.Blue
End Sub
<编辑:当单元格值更改时,我需要转换每个单元格的颜色更改。
我终于设法按照以下方式执行了!
- 你需要处理两个事件:
-
GridView.CellValueChanged
-
GridView.CustomDrawCell
-
- 您需要跟踪每个更改的单元格的索引。所以,我们需要一个列表
在其中创建一个类和三个字段。
公共类更新值
'UC表示已更新C $
公共属性UCFocusedRow As Integer
公共属性UCFocusedColumnIndex As Integer
公共属性UCFieldName As String
公共子新()
UCFocusedRow = -1
UCFocusedColumnIndex = -1
UCFieldName = String.Empty
结束Sub
结束类
初始化 Form1_Load
函数
Public lst As List(Of UpdatedCell)=新列表(UpdatedCell) ()
现在,在 GridView.CellValueChanged
事件,请执行以下操作:
Private Sub grdView_CellValueChanged(sender As Object,e As DevExpress.XtraGrid.Views.Base.CellValueChangedEventArgs)
Dim currCell As New UpdatedCell
currCel l.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
对于每个c作为UpdatedCell在lst
如果e.RowHandle = c.UCFocusedRow和
e.Column。 AbsoluteIndex = c.UCFocusedColumnIndex和
e.Column.FieldName = c.UCFieldName然后
e.Appearance.BackColor = Color.Yellow
Else
如果不是e.Appearance.BackColor = Color.Yellow然后
e.Appearance.BackColor = prevColor
结束如果
结束如果
下一个
End Sub
请注意,参数 e As RowCellCustomDrawEventArgs
包含所有必需的信息。我们只需要关心编辑的单元格索引,因为每次更改行/列焦点时, GridView.CustomDrawCell
调用
查看结果。
之前
而之后
注意黄色单元格具有不同的值我更改了使用inline / inplace编辑器。
谢谢
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.
I finally managed to do it in the following way!
- you need to handle two events:
GridView.CellValueChanged
GridView.CustomDrawCell
- 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
这篇关于VB.NET XtraGrid修改其值后更改单元格颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!