How to set a column in a datagridview as a picture column

Posted on 2011-10-16
Last Modified: 2013-12-16

I need to make one of the columns of my datagridview a picture column.
I tried in several ways but fail.

The column which I need to display as pictures contains the path to image. When I display a column normally i can see the path to where the image file is located. however, I would like it to show an image instead of the path to an image. How to do this?

Here's my code:
private void buttonExecuteSearch_MA_Items_Click(object sender, EventArgs e)
            string connectionString = "my String";
            SqlConnection conn = new SqlConnection(connectionString);

            string sql = "SELECT * FROM MA_Items WHERE 1=1 ";
            // Create new DataAdapter
            using (SqlDataAdapter a = new SqlDataAdapter(sql, conn))
                DataTable t = new DataTable();

                dataGridView1.DataSource = t;
                dataGridView1.CellFormatting +=
            new System.Windows.Forms.DataGridViewCellFormattingEventHandler(


        private void dataGridView1_CellFormatting(object sender,
            System.Windows.Forms.DataGridViewCellFormattingEventArgs e)
                if (dataGridView1.Columns[e.ColumnIndex].Name.Equals("Picture"))
                    DataGridViewImageColumn ColumnImage = new DataGridViewImageColumn();
                    ColumnImage.Name = "Picture";
                    ColumnImage.HeaderText = "Picture";
                    ColumnImage.ImageLayout = DataGridViewImageCellLayout.Zoom;
                    ColumnImage.Width = 40;

                    int i = 0;
                    foreach (DataRow row in ds.Tables["MA_Items"].Rows)
                        DataGridViewImageCell ImgCell = new DataGridViewImageCell();
                        ImgCell = (DataGridViewImageCell)dataGridView1.Rows[i].Cells["Picture"];
                        ImgCell.Value = Image.FromFile(e.Value.ToString());
                        i += 1;

Open in new window

Question by:Zaurb
    LVL 29

    Expert Comment

    I'd try to use the following approach:

    Basically, you read images (from file etc), and set them in a grid. Using DataGridViewImageColumn class.

    Accepted Solution

    I did something similar a few days ago. The difference is I dynamically created a table, with C# within an HTL file.

    TableRow TablaRow0 = new TableRow();
    TableCell Noti = new TableCell();
    Noti.Width = 80;
    Noti.VerticalAlign = VerticalAlign.Top;
    System.Web.UI.WebControls.Image img = new System.Web.UI.WebControls.Image();
    img.ID = "id";
    img.ImageUrl = "" + node["img"].InnerText;
    Noti.RowSpan = 4;

    Open in new window

    With System.Web.UI.WebControls.Image img = new System.Web.UI.WebControls.Image(); I'm able to manipulate the path as an actual image.

    I'm reading the image file name from an XML file.

    With img.ImageUrl = "" + node["img"].InnerText; I select the path + the image file name from an xml file, to find the image.

    You could just use your path.

    Hope this helped at least a bit.
    LVL 1

    Author Closing Comment

    Many Thanks!

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Highfive Gives IT Their Time Back

    Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

    Recently while returning home from work my wife (another .NET developer) was murmuring something. On further poking she said that she has been assigned a task where she has to serialize and deserialize objects and she is afraid of serialization. Wha…
    Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
    Hi everyone! This is Experts Exchange customer support.  This quick video will show you how to change your primary email address.  If you have any questions, then please Write a Comment below!
    In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

    737 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    16 Experts available now in Live!

    Get 1:1 Help Now