Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Add images to DataGridView (to specific column per each row) Dynamically

Using C# and Webforms, the code below:

DataGridViewImageColumn img = new DataGridViewImageColumn();
string path = "path" + file;
Image image = Image.FromFile(path);
img.Image = image;
DataGridView.Columns.Add(img);
img.HeaderText = "Picture";
img.Name = "picture";

My problem is the next one. This code is not working... DataGridview is not showing the image. When I debugged this code, the path is correct, and I load the Image properly in img element. Even more, when I´m using several images, the DataGridView is showing the same number of columns that I´m adding. But not any image at all is displayed, I only can see the typical white square within red cross...

What is the problem?

like image 478
Elias MP Avatar asked Jan 10 '16 16:01

Elias MP


2 Answers

You can add the image(s) to the Cells like this:

dataGridView1[yourColumn, yourRow].Value = Image.FromFile(path); 

Of course you can use any other image source.. This way you can load different images to each Row..

Note: If you really only want to add the same image to each Row you can do that with your code, but you need to add real rows.

It will not show in the last row, when AllowUserToAddRows is on but will show on all other rows you add..

like image 110
TaW Avatar answered Nov 14 '22 21:11

TaW


Just Sharing the final code solution was by @TaW Someone asked me for it:

private void dataGridView1_CellContentDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            codigo = int.Parse(dataGridView1.CurrentRow.Cells[0].Value.ToString());

            Piso formPiso = new Piso();

            var consultaPisoAlquiler = from piso in contexto.pisosAlquiler
                                       where piso.codigo == codigo
                                       select new
                                       {
                                           fotos = piso.fotos.Split(','),
                                           zona = piso.zona,
                                           mm = piso.mm,
                                           descripcion = piso.descripcion,
                                           precio = piso.precio,
                                           estado = piso.estado
                                       };

            formPiso.txtCodigo.Text = codigo.ToString();
            formPiso.txtDescripcion.Text = consultaPisoAlquiler.First().descripcion.ToString();
            formPiso.txtEstado.Text = consultaPisoAlquiler.First().estado.ToString();
            formPiso.txtMm.Text = consultaPisoAlquiler.First().mm.ToString();
            formPiso.txtPrecio.Text = consultaPisoAlquiler.First().precio.ToString();
            formPiso.txtZona.Text = consultaPisoAlquiler.First().zona.ToString();

            formPiso.DGVPisoFotos.AutoGenerateColumns = false;
            formPiso.DGVPisoFotos.Columns.Clear();
            DataGridViewImageColumn img = new DataGridViewImageColumn();
            img.Width = 230;
            img.HeaderText = "Fotos";
            formPiso.DGVPisoFotos.Columns.Add(img);
            int i = 0;
            foreach (var foto in consultaPisoAlquiler.First().fotos)
            {
                formPiso.DGVPisoFotos.Rows.Add();
                string path = "E:/WorkSpaces/MVS/ProyectoJoseph/" + foto;
                Bitmap image = (Bitmap)Image.FromFile(path);
                Bitmap imageRS = new Bitmap(image, new Size(230, 230));
                formPiso.DGVPisoFotos[0, i].Value = imageRS;
                formPiso.DGVPisoFotos.Rows[i].Height = 230;
                i++;
            }

            formPiso.ShowDialog();
}
like image 21
Elias MP Avatar answered Nov 14 '22 22:11

Elias MP