Showing posts with label DataGridView. Show all posts
Showing posts with label DataGridView. Show all posts

Tuesday, May 22, 2012

Having Text and Image in One Single Column of DataGridView


DataGridView's Cell_Painting event could be utilized to achieve the above. You need a DataGridView and ImageList in which an 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;
    }

}
 
Refer DataGridView FAQ in the following Thread:
http://social.msdn.microsoft.com/Forums/en-US/winformsdatacontrols/thread/b5ab78d6-a760-4f29-ac89-46bad51ba30a