0

In our application we are using datagridview control to show some data. In the gridview one column is DataGridViewImageColumn.

In the CellFormatting event we are setting some images like

e.CellStyle.BackColor = Color.Red;
e.Value = Properties.Resources.Triangle

Where Triangle is a bitmap resource and the image is transparent. When we set the color as Red the transparent portion of the image will show the color and is working fine.

Now we have to show some text over the image. So is there any way to show a text over a transparent image which is shown in the DataGridViewImageColumn.?

Mahesh KP
  • 6,248
  • 13
  • 50
  • 71

1 Answers1

2

No need to mess with the Image.

Instead you can control the painting of the cell by yourself, maybe like this:

private void dataGridView1_CellPainting(object sender,
                                        DataGridViewCellPaintingEventArgs e)
{
   if (e.ColumnIndex == yourImageColumnIndex)
   {
     e.PaintBackground(e.ClipBounds, true);
     e.PaintContent(e.ClipBounds);
     e.Graphics.DrawString(yourText, dataGridView1.Font, Brushes.Yellow,
                                     e.CellBounds.X, e.CellBounds.Y);
     e.Handled = true;  
   }
}

As you can see, most of the work is done by the system; you only need to add one line to draw your text. You most certainly will want to use a different font, of course and maybe change all other params as well. Please stay within the e.CellBounds rectangle.

You may want to habe a look at the rich set of data in the event's aguments..

If your text depends on the row, you can use the e.RowIndex param to get at the right text to display for each cell.

TaW
  • 53,122
  • 8
  • 69
  • 111