locked
DataGridView Image Column

    Question

  • Hi,

     

    How do I display a certain image in the datagridview relevant to the data from a dataset?

    e.g. if the dataset value is 'ok' then myimg.jpg should display in the DataGridView imgColumn.

     

    Below is my code to date...Is it correct? How do I reference the image? Thanks.

     

    Code Snippet

    foreach (DataRow theRow in this.myDataSet.Tables["statusCol"].Rows)

    {

               if(theRow == "ok")

               {

                    dataGridView1.Columns["ImgCol"] = "myimg.jpg";

               }

               else

               {

                   dataGridView1.Columns["ImgCol"] = "newimg.jpg";

               }

    }

     

     

    Friday, July 06, 2007 8:50 AM

Answers

  • Below is my code to date. The column should display an image based on the value of another column.

     

    Code Snippet

    private void dataGridView1_CellPainting(object sender, DataGridViewCellPaintingEventArgs e)

    {

    if (e.ColumnIndex == 2 && e.RowIndex > -1)

    {

    DataGridViewImageCell cell = (DataGridViewImageCell)dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex];

    if (cell.Value.ToString() == "Not OK")

    {

    if (e.ColumnIndex == 3)

    {

    e.PaintBackground(e.CellBounds, true);

    Image img = Properties.Resources.cross;

    e.Graphics.DrawImage(img, e.CellBounds);

    e.Handled = true;

    }

    }

    }

    }

     

    Monday, July 09, 2007 10:33 AM

All replies

  • Hi,

     

    Based on your post, you can display images in the datagridview from a dataset . I recommend you filter data to display specific image in datagridview. Here are some articles in Msdn about using datagridview control and Ado.net .

    Using Ado.net to access data: http://msdn2.microsoft.com/en-us/library/0wxwcakt(vs.71).aspx

    Using datagridview control: http://msdn2.microsoft.com/en-us/library/k39d6s23.aspx

     

    Here is code how to display image from database in datagridview. Hope this can help you.

               

    Code Snippet

    string select = "SELECT EmployeeID, FirstName, LastName, Photo, IsNull(ReportsTo,0) as       

                ReportsTo FROM Employees";

     

                SqlDataAdapter dataadapter1 = new SqlDataAdapter(select, con);

                Dataadapter1.Fill(dataset1, "Employees");

     

                DataGridViewImageColumn photoColumn = new DataGridViewImageColumn();

                photoColumn.DataPropertyName = "Photo";

                photoColumn.Width = 200;

                photoColumn.HeaderText = "Image";

                photoColumn.ReadOnly = true;

                photoColumn.ImageLayout = DataGridViewImageCellLayout.Normal;

                dataGridView1.Columns.Add(photoColumn);           

               dataGridView1.DataSource = dataset1.Tables["Employees"];

     

     

    Good luck

    Monday, July 09, 2007 4:29 AM
  • Below is my code to date. The column should display an image based on the value of another column.

     

    Code Snippet

    private void dataGridView1_CellPainting(object sender, DataGridViewCellPaintingEventArgs e)

    {

    if (e.ColumnIndex == 2 && e.RowIndex > -1)

    {

    DataGridViewImageCell cell = (DataGridViewImageCell)dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex];

    if (cell.Value.ToString() == "Not OK")

    {

    if (e.ColumnIndex == 3)

    {

    e.PaintBackground(e.CellBounds, true);

    Image img = Properties.Resources.cross;

    e.Graphics.DrawImage(img, e.CellBounds);

    e.Handled = true;

    }

    }

    }

    }

     

    Monday, July 09, 2007 10:33 AM