如何在DevExpress XtraGrid 控件中显示自定义行指示符图标?
This example演示了在RowIndicator列中显示文本。但我想根据某些条件显示来自Resources的自定义图像/图标。
在this example中,会显示错误图标。
这是我的代码:
Private cObj_ImageList As ImageList
cObj_ImageList = New ImageList()
cObj_ImageList.Images.Add(My.Resources.lock_red)
cObj_ImageList.Images.Add(My.Resources.unlock_blue)
Private Sub GridView_CustomDrawRowIndicator(sender As Object, e As RowIndicatorCustomDrawEventArgs)
If e.Info.IsRowIndicator And e.RowHandle >= 0 Then
Try
For Each lObj_ChngdRow As ChangedRow In cObj_Lst_ChngdRows
If lObj_ChngdRow.CRRowHandle = e.RowHandle And lObj_ChngdRow.IsEditable Then
'e.Graphics.DrawIcon(cObj_RowStateUnlock, 0, 0)
'e.Graphics.DrawImageUnscaled(My.Resources.unlock_blue, 0, 0)
e.Info.ImageIndex = -1
e.Painter.DrawObject(e.Info)
Dim rec As Rectangle = e.Bounds
rec.Inflate(-1, -1)
Dim x1 As Integer = rec.X + (rec.Width - cObj_ImageList.ImageSize.Width) / 2
Dim y1 As Integer = rec.Y + (rec.Height - cObj_ImageList.ImageSize.Height) / 2
e.Graphics.DrawImageUnscaled(cObj_ImageList.Images(1), x1, y1)
e.Handled = True
End If
Next
Catch ex As Exception
End Try
End If
End Sub
这仅显示一行的图标。我想显示所有行。但是,当点击按钮结帐时,我想要更改此图标。
编辑1:
正如nempoBu4解释的那样,我将代码修改为更简单:
Private Sub GridView_CustomDrawRowIndicator(sender As Object, e As RowIndicatorCustomDrawEventArgs)
Try
If e.Info.IsRowIndicator And e.RowHandle >= 0 Then
e.Handled = True
e.Graphics.DrawImageUnscaled(cObj_ImageList.Images(0), e.Bounds.X, e.Bounds.Y)
Dim lObj_ChngdRow = cObj_Lst_ChngdRows.Find(Function(item) item.CRRowHandle = e.RowHandle AndAlso item.IsEditable)
If Not lObj_ChngdRow Is Nothing Then
e.Graphics.DrawImageUnscaled(cObj_ImageList.Images(1), e.Bounds.X, e.Bounds.Y)
End If
End If
Catch ex As Exception
End Try
End Sub
请参阅随附的屏幕截图。
红色和蓝色图标在彼此之上。我想我必须清除ImageList然后再添加它们。
答案 0 :(得分:2)
CustomDrawRowIndicator
事件分别针对每一行触发,因此您无需在cObj_Lst_ChngdRows
集合中使用循环遍历所有对象。在将对象添加到cObj_Lst_ChngdRows
集合后,您还需要使行指示符无效。为此,您需要使用GridView.InvalidateRowIndicator
方法
这是一个例子:
Private Sub CheckoutButton_Click(sender As Object, e As EventArgs)
Dim rowHandle = gvException.FocusedRowHandle
Dim row = New ChangedRow()
row.CRRowHandle = rowHandle
row.IsEditable = True
cObj_Lst_ChngdRows.Add(row)
gvException.InvalidateRowIndicator(rowHandle)
End Sub
Private Sub GridView_CustomDrawRowIndicator(sender As Object, e As RowIndicatorCustomDrawEventArgs)
If e.Info.IsRowIndicator AndAlso e.RowHandle >= 0 Then
Try
Dim lObj_ChngdRow = cObj_Lst_ChngdRows.Find(Function(item) item.CRRowHandle = e.RowHandle AndAlso item.IsEditable)
If Not lObj_ChngdRow Is Nothing Then
e.Info.ImageIndex = -1
e.Painter.DrawObject(e.Info)
Dim rec As Rectangle = e.Bounds
rec.Inflate(-1, -1)
Dim x1 As Integer = rec.X + (rec.Width - cObj_ImageList.ImageSize.Width) / 2
Dim y1 As Integer = rec.Y + (rec.Height - cObj_ImageList.ImageSize.Height) / 2
e.Graphics.DrawImageUnscaled(cObj_ImageList.Images(1), x1, y1)
e.Handled = True
End If
Catch ex As Exception
End Try
End If
End Sub
编辑1
您要拨打e.Graphics.DrawImageUnscaled
方法两次。您只需要调用此方法一次
这是一个例子:
Private Sub GridView_CustomDrawRowIndicator(sender As Object, e As RowIndicatorCustomDrawEventArgs)
Try
If e.Info.IsRowIndicator And e.RowHandle >= 0 Then
e.Handled = True
Dim lObj_ChngdRow = cObj_Lst_ChngdRows.Find(Function(item) item.CRRowHandle = e.RowHandle AndAlso item.IsEditable)
Dim imageIndex As Integer
If lObj_ChngdRow Is Nothing Then
imageIndex = 0
Else
imageIndex = 1
End If
e.Graphics.DrawImageUnscaled(cObj_ImageList.Images(imageIndex), e.Bounds.X, e.Bounds.Y)
End If
Catch ex As Exception
End Try
End Sub