【问题标题】:vb.net datagridview image column (change image - conditional)vb.net datagridview 图像列(更改图像 - 有条件)
【发布时间】: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

That's what appears to me now

显然,它没有应用 IF 语句,有什么问题吗?我错过了什么吗?

希望这个想法足够清晰。

任何帮助将不胜感激。 谢谢!

【问题讨论】:

  • 您没有将 imgcol.name 设置为“img”
  • a) cellFormatting 事件或 RowPrePaint 事件将是设置图像的更好位置,而不是循环遍历所有行。 b)更大的问题是您正在为每一行创建一个新的图像对象。如果有 300 个红色行,则创建 300 个红色图像。最终应用程序将耗尽资源
  • 资源耗尽是什么意思?
  • 是什么让我使用循环,是根据我可以确定使用哪个图像的结果计算每行时间跨度(接收日期和今天)。我从未尝试过 RowPrepaint 事件或 CellFormating 事件,它可以完成这项工作吗?请举例?谢谢!

标签: vb.net image datagridview


【解决方案1】:

提供的快照未显示列 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

我添加了日期时间转换检查

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

【讨论】:

  • 谢谢,我已经上传了一张包含收到日期的图片。尝试了您的解决方案,但没有影响。谢谢!
  • @MohammedAbdulameer,您确定来自 ac_receiveddt 的单元格值已转换为正确接收的 DateTime。您是否设置了断点并对其进行了调试。我这样说是因为我已经在我的 PC 上尝试过它并且它运行良好。我已经更新了代码来测试它是否可以转换。
  • 您是否收到任何错误消息,例如“不是日期时间”?
  • 不,它工作正常,结果相同我在想什么,我正在从 ORACLE 数据库中检索日期,这会影响我检查 datagridview 单元格的方式吗?换句话说:应该以其他方式完成吗?我什至尝试从datagridview(设计视图)中删除“接收日期”列,并使用编码添加了一个名为“新”的新列,我做了同样的事情,希望在代码中,事情会改变,但不幸的是..还是一样。感谢您的帮助
  • 在If difference.Days
猜你喜欢
  • 2020-04-01
  • 1970-01-01
  • 2012-01-01
  • 1970-01-01
  • 2013-09-28
  • 1970-01-01
  • 2020-12-18
  • 2011-10-02
  • 1970-01-01
相关资源
最近更新 更多