【发布时间】:2017-02-14 13:59:38
【问题描述】:
我在我的 vb.net datagridview 中的图像列上苦苦挣扎。 我想要做的就是根据条件时间跨度更改图像。
这里是完整的代码:
con.Open()
da.SelectCommand = New OleDbCommand(Q, con)
da.Fill(ds)
da.Fill(dt)
DataGridView1.DataSource = dt
con.Close()
Dim receivedfrom As Date = Convert.ToDateTime(DateTimePicker1.Value)
Dim receivedto As Date = Convert.ToDateTime(DateTimePicker2.Value)
Dim difference As TimeSpan
Dim received As Date
Dim today As Date = today
Dim imgcol As New DataGridViewImageColumn()
Dim inImg As Image = My.Resources.red
imgcol.Image = inImg
DataGridView1.Columns.Add(imgcol)
imgcol.HeaderText = ""
imgcol.Name = "img"
imgcol.DataPropertyName = "img"
With DataGridView1
.Columns("img").DisplayIndex = 1
.Columns("img").Width = 28
.Columns("AC_RECEIVEDDT").DisplayIndex = 2
End With
For rowIndex = 0 To DataGridView1.RowCount - 1
received = DataGridView1.Rows(rowIndex).Cells("AC_RECEIVEDDT").Value
difference = today.Subtract(received)
If difference.Days < 2 Then
DataGridView1.Rows(rowIndex).Cells("img").Value = My.Resources.green
ElseIf difference.Days = 2 Then
DataGridView1.Rows(rowIndex).Cells("img").Value = My.Resources.yellow
Else
DataGridView1.Rows(rowIndex).Cells("img").Value = My.Resources.red
End If
Next
现在,当我打开应用程序时,所有图像都只有红色。
我添加了列名 =“img”,这就是我已经获得图像的原因,但我的问题是所有图像都没有根据收到的日期值进行更改,所有图像都显示为红色。
我的计划是将时间跨度少于 2 天的案例以绿色图像显示为可接受的处理时间。等于 2 = 黄色图像。超过 2 天 = 红色。
我现在确实有所有行的红色图像,但它没有根据 if 语句进行更改,所有行都有红色,因为我在图像列中分配了它。
目标:
This is a DGV list with conditional status, that's exactly what i want to do
显然,它没有应用 IF 语句,有什么问题吗?我错过了什么吗?
希望这个想法足够清晰。
任何帮助将不胜感激。 谢谢!
【问题讨论】:
-
您没有将 imgcol.name 设置为“img”
-
a) cellFormatting 事件或 RowPrePaint 事件将是设置图像的更好位置,而不是循环遍历所有行。 b)更大的问题是您正在为每一行创建一个新的图像对象。如果有 300 个红色行,则创建 300 个红色图像。最终应用程序将耗尽资源
-
资源耗尽是什么意思?
-
是什么让我使用循环,是根据我可以确定使用哪个图像的结果计算每行时间跨度(接收日期和今天)。我从未尝试过 RowPrepaint 事件或 CellFormating 事件,它可以完成这项工作吗?请举例?谢谢!
标签: vb.net image datagridview