Xceed Grid for WinForms v4.3 Documentation
How to display a picture in a cell

Welcome to Xceed Grid for WinForms v4.3 > Task-Based Help > Appearance > How to display a picture in a cell

By default, Xceed Grid for WinForms will not display images in cells. In most circumstances, images are stored as byte arrays in databases therefore, in order to display them in a cell, you have 2 options:

  1. Use the GridImageBox as a cell viewer.

Demonstration

The following example demonstrates how to use the GridImageBox cell viewer to display an image in a cell. For the purposes of this example, we will use the "Employees" table from the Northwind database.

VB.NET Copy Code

Dim grid As New GridControl()

Me.Controls.Add(grid)
grid.Dock = DockStyle.Fill

grid.FixedHeaderRows.Add(New ColumnManagerRow())   

grid.SetDataBinding(DataSet11, "Employees")

grid.Columns("Photo").VisibleIndex = 0
grid.Columns("Photo").CellViewer = New Xceed.Grid.Editors.GridImageBox()
                       
C# Copy Code

GridControl grid = new GridControl();

this.Controls.Add( grid );
grid.Dock = DockStyle.Fill;

grid.FixedHeaderRows.Add( new ColumnManagerRow() );
   
grid.SetDataBinding( DataSet11, "Employees" );

grid.Columns[ "Photo" ].VisibleIndex = 0;
grid.Columns[ "Photo" ].CellViewer = new Xceed.Grid.Editors.GridImageBox();
                       
  1. Create a stream from the byte array and a bitmap from the stream in order to retrieve the image. Because the image will not be automatically displayed, you will also need to create a class that derives from the DataCell class (and from the DataRow class!) and override the PaintForeground method in order to display the images.

Demonstration

The following example demonstrates the implementation required in the PaintForeground method to retrieve and display images in a cell.

VB.NET Copy Code

Protected Overrides Sub PaintForeground( ByVal e As GridPaintEventArgs )

  If Me.Value Is Byte() Then

    ' We start the offset at 78 because we are retrieving the images
    ' from the Northwind database. Under normal circumstances, the
    ' offset would be 0.
    Dim stream As New MemoryStream( CType( Me.Value, Byte() ), 78, _
                                  CType( Me.Value, Byte() ).Length - 78 )

    Dim bitmap As New Bitmap( stream )

    e.Graphics.DrawImage( bitmap, e.DisplayRectangle )

    bitmap.Dispose()
    stream.Close()     
  Else
    MyBase.PaintForeground( e )
  End If     
End Sub
                       
C# Copy Code

protected override void PaintForeground( GridPaintEventArgs e )
{
  if( this.Value is byte[] )
  {
    // We start the offset at 78 because we are retrieving the images
    // from the Northwind database. Under normal circumstances, the
    // offset would be 0.
    using( MemoryStream stream = new MemoryStream( ( byte[] )this.Value, 78,
                                                   ( ( byte[] )this.Value ).Length - 78 ) )
    {
      using( Bitmap bitmap = new Bitmap( stream ) )
      {            
        e.Graphics.DrawImage( bitmap, e.DisplayRectangle );           
      }
    }
  }
  else
  {
    base.PaintForeground( e );        
  }     
}