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







Installing the Visual Studio 2005 Image Library

The Visual Studio 2005 Image Library is copied to your computer when you install Visual Studio. To access the files in the image library, you must extract them from the file VS2005ImageLibrary.zip.

To install the Visual Studio 2005 Image Library

  1. Locate the file VS2005ImageLibrary.zip. This file is normally installed in \...\Program Files\Microsoft Visual Studio 8\Common7\VS2005ImageLibrary\.
  2. Right-click VS2005ImageLibrary.zip and click Extract All.
    The Extraction Wizard appears.
  3. Follow the directions in the wizard to extract the images.
Refer MSDN Link