Flávio Costa
Flávio Costa

Reputation: 957

How do I add an image in my DataGridViewImageColumn?

I have a field DataGridViewImageColumn, and for each line of the field, depending on a condition, I add a different image. Anyone know how I can do this in Windows Forms?

if (dgvAndon.Rows[e.RowIndex].Cells["urgencyOrder"].ToString() == "1")
{
   //Here I want to add the image in the image property field DataGridViewImageColumn.
}

Upvotes: 14

Views: 51340

Answers (5)

MOHAMED EL JIHAOUI
MOHAMED EL JIHAOUI

Reputation: 101

add the image as ressource in your project , and apply the code bellow

DataGridViewImageColumn btnDel = new DataGridViewImageColumn();
btnDel.Name = "DelCourrier";
btnDel.HeaderText = "";
btnDel.Image = Properties.Resources.delete;// delete is the name of the image added as ressource
dataGridView1.Columns.Add(btnDel);

Upvotes: 0

string FileName = null;

OpenFileDialog openFileDialog = new OpenFileDialog();
openFileDialog.RestoreDirectory = true;

openFileDialog.Filter = "All picture files (*.BMP;*.JPG;*.GIF)|*.BMP;*.JPG;*.GIF";

if (openFileDialog.ShowDialog() == DialogResult.OK)
{
    FileName = openFileDialog.FileName;
    //pictureBox2.Image = Image.FromFile(FileName);
}

Upvotes: 0

Koorosh
Koorosh

Reputation: 475

use this code:

        DataGridViewImageColumn iconColumn = new DataGridViewImageColumn();
        iconColumn.Name = "AirplaneImage";
        iconColumn.HeaderText = "Airplane Image";
        dataGridView1.Columns.Insert(5, iconColumn);

        for (int row = 0; row < dataGridView1.Rows.Count - 1; row++)
        {
            Bitmap bmp = new Bitmap(Application.StartupPath + "\\Data\\AirPlaneData\\" + dt.Rows[row][4]);
            ((DataGridViewImageCell)dataGridView1.Rows[row].Cells[5]).Value = bmp;
        }

Upvotes: 3

ketan italiya
ketan italiya

Reputation: 296

use this code

 protected void gridView1_RowDataBound(Object sender, GridViewRowEventArgs args)
    {
     if(args.Row.RowType == DataControlRowType.DataRow)
     {
      Image img = (Image) e.Row.FindControl("Image1");
      img.ImageUrl = setImageURLHere;
     }
    }

Upvotes: 0

Manoj
Manoj

Reputation: 883

  1. Add your image in Resources.resx under properties folder. (ex. Picture1.jpeg)
  2. Add a DataGridViewImageColumn in your DataGridView
  3. Add image this way:

    for (int row = 0; row <= [YourDataGridViewName].Rows.Count - 1; row++)
    {
        ((DataGridViewImageCell)gvFiles.Rows[row].Cells[1]).Value = Properties.Resources.Picture1
    }
    

Upvotes: 15

Related Questions