簡體   English   中英

Datagridview不更改單元格中的圖像

[英]Datagridview not changing image in cell

我正在使用VB.NET以Windows形式使用DataGridViewObject。 我有三列需要顯示圖標。 但是,基於行中的某些信息,該圖標將不會顯示。 我的問題是,更改值后圖像不會改變。 基本上,我這樣定義我的列:

Dim column1 As DataGridViewImageColumn = New DataGridViewImageColumn()
column1.Image = My.Resources.image1
column1.Width = 30
dw.Columns.Add(column1)

Dim column2 As DataGridViewImageColumn = New DataGridViewImageColumn()
column2.Image = My.Resources.image2
column2.Width = 30
dw.Columns.Add(column2)

Dim column3 As DataGridViewImageColumn = New DataGridViewImageColumn()
column3.Image = My.Resources.image3
column3.Width = 30
dw.Columns.Add(column3)

填充數據后,我將遍歷各行,如果不想顯示該行中的圖像,請執行以下操作:

Dim cell1 As DataGridViewImageCell = row.Cells(9)
Dim cell2 As DataGridViewImageCell = row.Cells(10)
Dim cell3 As DataGridViewImageCell = row.Cells(11)

cell1.Value = Nothing
cell2.Value = Nothing
cell3.Value = Nothing

但是,我的圖像仍然存在。 有人知道我在想什么嗎?

您正在使用一些未綁定的DataGridViewImageColumn ,如文檔中所述, Image屬性指定當列未綁定數據時在單元格中顯示的圖像。

因此,通過將單元格值設置為null,可以強制該單元格顯示Image屬性。

解決問題:

  1. 為您的列設置column.DefaultCellStyle.NullValue = Nothing
  2. 不要設置Image屬性。
  3. 每次要顯示圖像時,請將圖像分配給單元格的“ Value屬性。

您可以在循環中或使用CellFormatting事件手動設置Cell的值。 例如:

Private Sub DataGridView1_CellFormatting(ByVal sender As System.Object, _
ByVal e As System.Windows.Forms.DataGridViewCellFormattingEventArgs) _
Handles DataGridView1.CellFormatting

    If (e.RowIndex >= 0 AndAlso e.ColumnIndex = 2) Then 
        If (e.RowIndex Mod 2 = 0) Then 'Use any criteria which you need, it's a test
            e.Value = My.Resources.Image1
        Else
            e.Value = DBNull.Value
            e.CellStyle.NullValue = Nothing
        End If
    End If

   ' Do the same for other image columns. 

End Sub

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM