Click here to Skip to main content
15,886,919 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
Hi,
Recently i got stuck to this question by an interviewer; since then i have been toiling to get through it. The question goes like this:

"How can you show an image in the datagridview in vb.net?"

Please do help me.
Thanks in advancee
Posted
Updated 11-May-17 9:37am
v2

 
Share this answer
 
Hello,

There's a DataGridViewColumn class just for that!

http://msdn.microsoft.com/en-us/library/2ab8kd75.aspx[^]
 
Share this answer
 
this is what I found; Thanks 9from ap.

Public Class Form1
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        DataGridView1.ColumnCount = 3
        DataGridView1.Columns(0).Name = "Product ID"
        DataGridView1.Columns(1).Name = "Product Name"
        DataGridView1.Columns(2).Name = "Product_Price"

        Dim row As String() = New String() {"1", "Product 1", "1000"}
        DataGridView1.Rows.Add(row)
        row = New String() {"2", "Product 2", "2000"}
        DataGridView1.Rows.Add(row)
        row = New String() {"3", "Product 3", "3000"}
        DataGridView1.Rows.Add(row)
        row = New String() {"4", "Product 4", "4000"}
        DataGridView1.Rows.Add(row)

        Dim img As New DataGridViewImageColumn()
        Dim inImg As Image = Image.FromFile("Image Path")
        img.Image = inImg
        DataGridView1.Columns.Add(img)
        img.HeaderText = "Image"
        img.Name = "img"

    End Sub
End Class
 
Share this answer
 
v2
I hope this will help for those developer have not yet resolved the problem. I made my own way of solution with simple and easy method.

1. For Single and direct Datagridview

Datagridview1.Rows(Row Index of Datagrid).Cells(Column Index of Datagrid).Value = "LOCATION OF THE IMAGE OR PROJECT RESOURCES"

Example:

Private Sub Datagridview1_RowsAdded(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewRowsAddedEventArgs) Handles Datagridview1.RowsAdded

Datagridview1.Rows(e.RowIndex).Cells(0).Value = PDP.My.Resources.Resources.myImage

End Sub

Private Sub dgViewTC_UserAddedRow(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewRowEventArgs) Handles dgViewTC.UserAddedRow

Datagridview1.Rows(e.Row.Index).Cells(0).Value = PDP.My.Resources.Resources.myImage

End Sub

NOTE: PDP.My.Resources.Resources.myImage is where the image located in my project.

2. For Multi Datagridview

Private Sub dgView1_UserAddedRow(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewRowEventArgs) Handles dgView1.UserAddedRow
CellViewImage(dgView1, e.Row.Index, 0)
End Sub

Private Sub dgView1_RowsAdded(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewRowsAddedEventArgs) Handles dgView1.RowsAdded
CellViewImage(dgView1, e.RowIndex, 0)
End Sub

Private Sub dgView2_UserAddedRow(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewRowEventArgs) Handles dgView2.UserAddedRow
CellViewImage(dgView2, e.Row.Index, 0)
End Sub

Private Sub dgView2_RowsAdded(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewRowsAddedEventArgs) Handles dgView2.RowsAdded
CellViewImage(dgView2, e.RowIndex, 0)
End Sub


Public Function CellViewImage(ByVal pDG As DataGridView, ByVal pRow As Integer, ByVal pCol As Integer) As Boolean
Try
pDG.Rows(pRow).Cells(pCol).Value = PDP.My.Resources.Resources.myImage
Return True
Catch ex As Exception
messagebox.show(ex)
Return False
End Try
End Function


HOPE THIS WILL HELP...
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900