VB.NET - How to read image from file name and display in DataGridView in windows application?

Asked By Eswaran Radhakrishnan on 16-Nov-09 01:26 PM
Hi all,

I have stored image file path in my ms-access table. I need to read this file path and display the image in GridView control in Windows application using vb.net.

How to display image from file in GridView of Windows application?

Thanks
r.e

adding image to datagridview - Venkat K replied to Eswaran Radhakrishnan on 16-Nov-09 09:56 PM

First, the column with the enum (the 'value column')must be included in the
grid, so that you can get the enum value for each row. So, set the Visible
property of that column to false, so it doesn't show.

Second, add an unbound image column to the grid, to act as the 'display
column'.

Third, create an event handler for the grid's DataBindingComplete event. We
could use the CellFormatting event, but that leads to continuous repainting
of the grid.

Fourth, add the following code to the DataBindingComplete event handler:

DataGridViewCell valueCell = null;
DataGridViewImageCell displayCell = null;
for (int row = 0; row < gridWidgets.Rows.Count; row++)
{
valueCell = gridWidgets[columnColor.Index, row];
displayCell = (DataGridViewImageCell)gridWidgets[columnGlyph.Index,
row];
int n = (int)valueCell.Value;
displayCell.Value = imageListWidgets.Images[n];
}

Thanks,

where to keep the enum values in GridView? - Eswaran Radhakrishnan replied to Venkat K on 17-Nov-09 04:29 AM

Hi,

I could not understand what you are trying to say to sovle my problem...
 Pls tell me and give me the full code if you don't mind then that should be better for me...

Thanks
r.e
Venkat K replied to Eswaran Radhakrishnan on 18-Nov-09 12:11 AM

In the above code i am using some enumerators to identify the images easily if you find it difficult to understand try to use this simple code to bind the image:

Assuming that you are storing Image path in the database and trying to bind the image of that path to the datagridview:

If you want path, simply bind the path of file to a column, you can format the image path to image in CellFormatting event, but folloiwing examples assumes your images are in debug/bin folder, since that is the path from where your exe file is, next you should have file name like image.jpg not ~/image.jpg

Private Sub DataGridView1_CellFormatting(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellFormattingEventArgs) Handles DataGridView1.CellFormatting
        If e.ColumnIndex = 2 Then 'assumed third column is for image change the value based on what column your image need to bind
            If e.Value IsNot Nothing And Not TypeOf e.Value Is Bitmap Then
                e.Value = Image.FromFile(IO.Path.Combine(Application.StartupPath, e.Value.ToString))
                e.FormattingApplied = True
            End If

        End If
    End Sub
Hope this will helps you!
Thanks,