how to give image to a particular cell in datagrid view in vb.net
I want to add rows in to a grid while running.
I added new rows by enabling add property of gridview
In that row one of the cell is an image .if i am adding one cell type as DataGridViewImageColumn.
then at adding new row, image cell shows only X(not showing image from Resourses).

Dani AI

Generated

A short clarification: the red "X" means the image cell tried to render something that was not a valid Image (or the image load threw an exception). The fix is to create the DataGridViewImageColumn once, and make sure every cell in that column gets an actual System.Drawing.Image object (either when you add rows programmatically or when the user starts a new row). If your values are file paths or strings, convert them to Images first — the grid will not automatically load a file path into an image.

Create the column once (for example in Form_Load) and set its value type/layout:

Dim imgCol As New DataGridViewImageColumn()
imgCol.Name = "Icon"
imgCol.HeaderText = "Icon"
imgCol.ValueType = GetType(System.Drawing.Image)
imgCol.ImageLayout = DataGridViewImageCellLayout.Zoom
DataGridView1.Columns.Add(imgCol)

When adding rows programmatically, supply an Image object for that column (or set the cell after adding the row):

Dim img As Image = LoadImageNoLock("C:\images\foo.png")   ' helper below
DataGridView1.Rows.Add("text for col1", img)              ' values must match column count
' -- or --
Dim r = DataGridView1.Rows(DataGridView1.Rows.Count - 1)
r.Cells("Icon").Value = img

If you allow the user to add rows (AllowUserToAddRows = True) use DefaultValuesNeeded to give the new-row placeholder a valid image so it doesn't show an X:

Private Sub DataGridView1_DefaultValuesNeeded(sender As Object, e As DataGridViewRowEventArgs) Handles DataGridView1.DefaultValuesNeeded
    e.Row.Cells("Icon").Value = My.Resources.DefaultImage
End Sub

Tips and cautions: avoid calling Image.FromFile repeatedly (it locks the file). Use a safe loader/cloner and cache images:

Private Function LoadImageNoLock(path As String) As Image
    Using fs As New IO.FileStream(path, IO.FileMode.Open, IO.FileAccess.Read)
        Dim tmp As Image = Image.FromStream(fs)
        Return New Bitmap(tmp)
    End Using
End Function

If the grid is data-bound and the source holds file paths, convert paths to images in CellFormatting (with caching) instead of storing plain strings in the image column. For : add the column only once (as you wanted) and use DefaultValuesNeeded for the user-added row; for the general MSDN guidance helps but you only need the column created once and then supply Image objects per-cell.

HI,
Go through this link, might you will get what you are looking for,
http://msdn.microsoft.com/en-us/library/2ab8kd75.aspx

BTW, Are you fatching images from database or you have created one foler in Solution Bar?
How are you fetching?. Need to be briefed

thank u for ur reply.
Images are not from database.it is from the folder.
The link u attached actually add a new column always.
my need is not like that.
i want to add column only once, add images always to the added column

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.