0

enter image description here

I have a DataGridViewTextBoxColumn in which i want add text and image in different rows according to some condition. For ex:-

If Row# == 1
   then add Text
and 
If Row# == 2
   then add Image.

I don't want to change the column type.

Is it possible to do this with DataGridViewTextBoxColumn ?

user2691432
  • 93
  • 1
  • 2
  • 10

1 Answers1

0

DataGridView's Cell_Painting event could be utilized to achieve the above. You need a DataGridView and ImageList in which is Image to be attached. The following simple code in event will be able to achieve this:

private void dataGridView1_CellPainting(object sender, DataGridViewCellPaintingEventArgs e)
 {
 
    if (e.RowIndex >= 0 && e.ColumnIndex == 0 && Convert.ToInt32(e.Value.ToString()) > 0)
    {
        e.PaintBackground(e.ClipBounds, false);
       dataGridView1[e.ColumnIndex, e.RowIndex].ToolTipText = e.Value.ToString();
       PointF p = e.CellBounds.Location;
       p.X += imageList1.ImageSize.Width;
 
      e.Graphics.DrawImage(imageList1.Images[0], e.CellBounds.X, e.CellBounds.Y, 16, 16);
       e.Graphics.DrawString(e.Value.ToString(), e.CellStyle.Font, Brushes.Black, p);
       e.Handled = true;
    }
 
}
Anant Dabhi
  • 10,864
  • 3
  • 31
  • 49