【发布时间】:2016-07-14 05:46:01
【问题描述】:
大家好,我正在做一个在 Visual Studio 2005 中创建的 Windows 窗体,它在 datagridview 中显示数据。我有一列“colImg”将显示 1 和 0。但是当 colImg 的单元格值为 0 时我需要显示红色图像,当值为 1 时显示绿色图像。我有一个代码但问题是它只是显示绿色的图像,但我的值为 0。我的代码有问题吗?
Private Sub grdView_CellFormatting(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellFormattingEventArgs) Handles grdView.CellFormatting
If grdView.Columns(e.ColumnIndex).Name.Equals("colImg") Then
Dim value As Integer
If TypeOf e.Value Is Integer Then
value = DirectCast(e.Value, Integer)
e.Value = My.Resources.Resources.NotYet
Else
For i As Integer = 0 To grdView.RowCount
If value = 0 Then
e.Value = My.Resources.Resources.Red
Else
e.Value = My.Resources.Resources.Green
End If
Next
End If
End If
【问题讨论】:
-
为什么要使用
For .. Next循环?我很确定嵌套的if正在做你所期望的。 -
我尝试在我的 datagridview 中循环这些值,但是当我没有使用循环时,结果仍然相同。值为 1 的单元格不显示为绿色
-
试试这个:
e.CellStyle.BackColor = If(e.Value=0, Color.Red, Color.Green)insideCellFormatting事件。 -
但我需要放的是图像而不是背景颜色。
标签: vb.net datagridview visual-studio-2005