vb.net datagridview图像列(更改图像-条件)

vb.net datagridview图像列(更改图像-条件),vb.net,image,datagridview,Vb.net,Image,Datagridview,我正在vb.net datagridview中处理图像列。 我想做的就是根据有条件的时间跨度改变图像 以下是完整的代码: con.Open() da.SelectCommand=新的OleDbCommand(Q,con) da.填充(ds) da.填充(dt) DataGridView1.DataSource=dt con.Close() Dim receivedfrom As Date=Convert.ToDateTime(DateTimePicker1.Value) Dim receive

我正在vb.net datagridview中处理图像列。 我想做的就是根据有条件的时间跨度改变图像

以下是完整的代码:

con.Open()
da.SelectCommand=新的OleDbCommand(Q,con)
da.填充(ds)
da.填充(dt)
DataGridView1.DataSource=dt
con.Close()
Dim receivedfrom As Date=Convert.ToDateTime(DateTimePicker1.Value)
Dim receivedto As Date=转换为.ToDateTime(DateTimePicker2.Value)
随时间跨度变化的微弱差异
作为日期收到的Dim
将今天变暗为日期=今天
将imgcol设置为新的DataGridViewImageColumn()
Dim inImg As Image=My.Resources.red
imgcol.Image=inImg
DataGridView1.Columns.Add(imgcol)
imgcol.HeaderText=“”
imgcol.Name=“img”
imgcol.DataPropertyName=“img”
使用DataGridView1
.列(“img”).DisplayIndex=1
.列(“img”)。宽度=28
.Columns(“AC_RECEIVEDDT”).DisplayIndex=2
以
对于rowIndex=0的数据,请访问DataGridView1.RowCount-1
received=DataGridView1.Rows(rowIndex).Cells(“AC_RECEIVEDDT”).值
差=今天。减去(收到)
如果差值小于2天,则
DataGridView1.Rows(rowIndex).Cells(“img”).Value=My.Resources.green
其他差异。则天数=2
DataGridView1.Rows(rowIndex).Cells(“img”).Value=My.Resources.yellow
其他的
DataGridView1.Rows(rowIndex).Cells(“img”).Value=My.Resources.red
如果结束

下一步
提供的快照不显示列ac_receiveddt的值。无论如何,假设它包含日期范围,并且您希望根据差异显示颜色。试试下面。使用
Days
而不是
TotalDays

If difference.Days < 2 Then
    DataGridView1.Rows(rowIndex).Cells("img").Value = My.Resources.Resource1.green
ElseIf difference.Days = 2 Then
    DataGridView1.Rows(rowIndex).Cells("img").Value = My.Resources.Resource1.yellow
End If
如果差值小于2天,则
DataGridView1.Rows(rowIndex).Cells(“img”).Value=My.Resources.Resource1.green
其他差异。则天数=2
DataGridView1.Rows(rowIndex).Cells(“img”).Value=My.Resources.Resource1.yellow
如果结束
我添加了日期时间转换的检查

For rowIndex = 0 To DataGridView1.RowCount - 1  
Dim rValue = DataGridView1.Rows(rowIndex).Cells("AC_RECEIVEDDT").Value
If DateTime.TryParse(rValue, received) Then
    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
Else
    MessageBox.Show("not a DateTime")
End If  
Next
对于DataGridView1.RowCount-1的rowIndex=0
Dim rValue=DataGridView1.Rows(rowIndex).Cells(“AC_RECEIVEDDT”).Value
如果是DateTime.TryParse(右值,已接收),则
差=今天。减去(收到)
如果差值小于2天,则
DataGridView1.Rows(rowIndex).Cells(“img”).Value=My.Resources.green
其他差异。则天数=2
DataGridView1.Rows(rowIndex).Cells(“img”).Value=My.Resources.yellow
其他的
DataGridView1.Rows(rowIndex).Cells(“img”).Value=My.Resources.red
如果结束
其他的
MessageBox.Show(“不是日期时间”)
如果结束
下一个

您没有将imgcol.name设置为“img'a)cellFormatting事件或RowPrePaint事件是设置图像的更好位置,而不是在所有行中循环。b) 更大的问题是,您正在为每一行创建一个新的图像对象。如果有300个红色行,则创建300个红色图像。最终,应用程序将耗尽资源您所说的耗尽资源是什么意思?我之所以使用循环,是因为根据结果计算每行时间跨度(接收日期和今天),我可以确定使用哪幅图像。我从来没有尝试过RowPrepaint事件或CellFormating事件,它会起作用吗?请举个例子好吗?谢谢谢谢,我上传了一张照片,里面也有收到的日期。尝试了您的解决方案,但没有效果。谢谢@MohammedAbdulameer,您确定ac_receiveddt中的单元格值已正确转换为DateTime received。您是否设置了断点并对其进行了调试。我这么说是因为我已经在我的电脑上试用过了,而且效果很好。我已经更新了代码以测试它是否转换。您是否收到任何错误消息,如“not a DateTime”?不,工作正常,结果与我所想的相同,我正在从ORACLE数据库检索日期,这会影响我检查datagridview单元格的方式吗?换言之:是否应该以另一种方式进行?我甚至尝试从datagridview(设计视图)中删除“received date”列,并使用编码添加了一个名为“new”的新列,我也做了同样的事情,希望在代码中,事情会有所改变,但不幸的是。。还是一样。感谢您的帮助在If difference.Days<2处设置一个断点,然后查看difference和difference.Days的值是多少