I have datagridview which display 3 columns. i would like to show icon in each rows with different by if column 3 = "OFFLINE" then the icon offline show else online show. the above is my code but not working. it is creating other column. please help me.

Chantra.

Public Sub _CreatePublishersGraphicsColumn()
Dim _IconColumn As New DataGridViewImageColumn()
Dim _IconColumn1 As New DataGridViewImageColumn()
With _IconColumn
.Image = frmAddCard.ImageList1.Images.Item(3)
.Name = "OFFLINE"
.Width = 20
.ReadOnly = True
.HeaderText = ""
End With
With _IconColumn1
.Image = frmAddCard.ImageList1.Images.Item(0)
.Name = "ONLINE"
.Width = 20
.ReadOnly = True
.HeaderText = ""
End With
Dim i As Integer
For i = 0 To frmAddCard.CardInStockView.RowCount - 1
Try
If frmAddCard.CardInStockView.Item(3, i).Value = "OFFLINE" Then
frmAddCard.CardInStockView.Columns.Insert(0, _IconColumn1)
Else
frmAddCard.CardInStockView.Columns.Insert(0, _IconColumn)
End If
Catch ex As Exception

End Try
Next
End Sub

How to: Display Images in Cells of the Windows Forms DataGridView Control?

Public Sub CreateGraphicsColumn()
    Dim treeIcon As New Icon(Me.GetType(), "tree.ico")
    Dim iconColumn As New DataGridViewImageColumn()
    With iconColumn
        .Image = treeIcon.ToBitmap()
        .Name = "Tree"
        .HeaderText = "Nice tree"
    End With
    dataGridView1.Columns.Insert(2, iconColumn)
End Sub

How to: Display Images in Cells of the Windows Forms DataGridView Control?

Public Sub CreateGraphicsColumn()
    Dim treeIcon As New Icon(Me.GetType(), "tree.ico")
    Dim iconColumn As New DataGridViewImageColumn()
    With iconColumn
        .Image = treeIcon.ToBitmap()
        .Name = "Tree"
        .HeaderText = "Nice tree"
    End With
    dataGridView1.Columns.Insert(2, iconColumn)
End Sub

Thank you for your reply, it is working as well. but what i prefer is each rows display with different icon. i got two icon.

Chantra

if the column for your icon is set as DataGridViewImageColumn then you could do:

For Each row As DataGridViewRow In DataGridView1.Rows
if row.Cells(3).Value = "OFFLINE" then
row.Cells(0).Value = My.Resources.offline
else
row.Cells(0).Value = My.Resources.online
end if
Next

note: you can do the condition also at runtime instead of looping trough. I have used the my.resources where i store my images. i just find it easier :$

if the column for your icon is set as DataGridViewImageColumn then you could do:

For Each row As DataGridViewRow In DataGridView1.Rows
if row.Cells(3).Value = "OFFLINE" then
row.Cells(0).Value = My.Resources.offline
else
row.Cells(0).Value = My.Resources.online
end if
Next

note: you can do the condition also at runtime instead of looping trough. I have used the my.resources where i store my images. i just find it easier :$

I already try this. but it is not working the result showing is System.Drawing.Bitmap

Please advice?

Thank you!

can u tell me what format your images have?

can u tell me what format your images have?

The Resource all images is *.ico

thanks.

ok you have created a datagridview with 3 columns. the first colum u want to show the icon i guess? if so please set this column to type imagecolumn.
the code you posted in the first post is wrong. First if you have just 3 columns then item(3) does not exist. it starts with 0 and your posted code will add a new column to your grid for each row. instead of this you do this:

For each row as datagridviewrow in frmAddCard.CardInStockView.Rows
Try
If row.cell(2).Value = "OFFLINE" Then
row.cell(0).Value=frmAddCard.ImageList1.Images.Item(3)
Else
row.cell(0).Value=frmAddCard.ImageList1.Images.Item(0)
End If
Catch ex As Exception

End Try
Next

ok you have created a datagridview with 3 columns. the first colum u want to show the icon i guess? if so please set this column to type imagecolumn.
the code you posted in the first post is wrong. First if you have just 3 columns then item(3) does not exist. it starts with 0 and your posted code will add a new column to your grid for each row. instead of this you do this:

For each row as datagridviewrow in frmAddCard.CardInStockView.Rows
Try
If row.cell(2).Value = "OFFLINE" Then
row.cell(0).Value=frmAddCard.ImageList1.Images.Item(3)
Else
row.cell(0).Value=frmAddCard.ImageList1.Images.Item(0)
End If
Catch ex As Exception

End Try
Next

Thank you very much! it is working as well.

Chantra

then please mark this thread as solved

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.