[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 588
  • Last Modified:

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

Hi!

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);
            conn.Open();

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

                dataGridView1.DataSource = t;
                dataGridView1.AutoResizeColumns();
                dataGridView1.CellFormatting +=
            new System.Windows.Forms.DataGridViewCellFormattingEventHandler(
            this.dataGridView1_CellFormatting);

            }
        }

        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;


                    dataGridView1.Columns.Add(ColumnImage);
                    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

0
Zaurb
Asked:
Zaurb
1 Solution
 
anarki_jimbelCommented:
I'd try to use the following approach:
http://msdn.microsoft.com/en-us/library/system.windows.forms.datagridviewimagecolumn(v=VS.80).aspx

Basically, you read images (from file etc), and set them in a grid. Using DataGridViewImageColumn class.
0
 
YushellCommented:
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 = "http://www.sample.com/folder/" + node["img"].InnerText;

Noti.Controls.Add(img);
TablaRow0.Cells.Add(Noti);
TableUNO.Rows.Add(TablaRow0);
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 = "http://www.sample.com/folder/" + 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.
0
 
ZaurbAuthor Commented:
Many Thanks!
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now