Link to home
Start Free TrialLog in
Avatar of countrymeister
countrymeister

asked on

display images in a datagridview column on a windows form

How do I display different images in a datagrid view. I have a windows form which has an image column called Indicator
I have a stored procedure which returns the image to display in a column called erp_image.

This column has the path to the image , example Images\red,gif
Under my root windows app I have folder called images, under which I have the respective images.
I am binding a dataset to the datagrid programmatically
example
 ds = DBManager.getList(txtFromCal, txtToCal, txtStatus, txtClient, txtFilter);
            dgView.DataSource = ds;
            dgView.AutoGenerateColumns = false;
            dgView.DataMember = "ERP";
                Status.DataPropertyName = "ERP_STATUS";
                Source.DataPropertyName = "ERP_SOURCE";
                Indicator.DataPropertyName = "ERP_IMAGE";
Avatar of shahprabal
shahprabal
Flag of United States of America image

Basically you would need to add the images to an image column in your dataset's datatable.
Check out :
http://msdn2.microsoft.com/en-us/library/system.windows.forms.datagridviewimagecolumn(VS.80).aspx
Avatar of countrymeister
countrymeister

ASKER

shahprabal,

The image column has to display different images based on the value of ERP_IMAGE.

I do have an image column but I need to bind an image at runtime
i have tried the following code and it works with an ImageList, but I loose the image when I sort on any column in the grid
DataGridViewCell valueCell = null;
                DataGridViewImageCell displayCell = null;
                for (int row = 0; row < dgView.Rows.Count; row++)
                {
                    valueCell = dgView[IndicatorImage.Index, row];
                    displayCell = (DataGridViewImageCell)dgView[Indicator.Index, row];
                    string n = (string)valueCell.Value;
                    //displayCell.Value = imageListIndicator.Images[n];
                    if (n == "~/images/blueofms.gif")
                        displayCell.Value = imageListIndicator.Images[0];
                    else if (n == "~/images/greenofms.gif")
                        displayCell.Value = imageListIndicator.Images[1];
                    else if (n == "~/images/redofms.gif")
                        displayCell.Value = imageListIndicator.Images[2];
                }
I have the same problem where i loose the images and it displays a red x, when i sort on any grid column.
ASKER CERTIFIED SOLUTION
Avatar of shahprabal
shahprabal
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial