Ticket A95
Visible to All Users

How to display an image from a data table in an Unbound View

created 21 years ago

Description:
I need to utilize an unbound grid, and display a photo from a database table.
The following code works, but I cannot get the grid to display a larger row size for each row containing a photo (some don't). Is there any way to do this, or alter the code to adjust the row size based on the photo size?

Delphi
procedure TfPregnancy.SampleTableViewPHOTOCustomDrawCell(Sender: TcxCustomGridTableView; ACanvas: TcxCanvas; AViewInfo: TcxGridTableDataCellViewInfo; var ADone: Boolean); var Photo : TPicture; begin Photo := TPicture.Create; try with Sapmle.DataSet do dmSample.GetPhoto( FieldByName 'IDNUMBER' ).AsString, Photo ); ACanvas.FillRect( AViewInfo.Bounds ); ACanvas.Draw( AViewInfo.ContentBounds.Left, AViewInfo.ContentBounds.Top, Photo.Graphic ); ADone := True; finally Photo.Free; end; end;


Answer:
We suggest populating the View's DataController with data by porting it from the BlobField using a stream. The following example demonstrates how to force the grid to display images in an ImageColumn when the DataController works in default loading mode.

Delphi
procedure TForm1.FormCreate(Sender: TObject); var I: Integer; ASStream, AFStream: TStream; AFileName: string; begin with <aView> do begin DataController.RecordCount := 6; Items[0].DataBinding.ValueTypeClass := TcxSmallintValueType; Items[1].DataBinding.ValueTypeClass := TcxStringValueType; for I := 0 to DataController.RecordCount - 1 do begin DataController.Values[I, 0] := I; if I in [0, 2, 4] then AFileName := '360_Modena.jpg' else AFileName := '360Modena.jpg'; AFStream := TFileStream.Create(AFileName, fmOpenRead); ASStream := TStringStream.Create(''); try ASStream.CopyFrom(AFStream, 0); DataController.Values[I, 1] := TStringStream(ASStream).DataString; finally AFStream.Free; ASStream.Free; end; end; end; end;

See also:
How to implement unbound checkboxes in a bound View
How to show a row number value in an unbound Grid column
How to set up an unbound item in a data-aware View
How to dynamically load image files into a GridView based upon the path stored in a particular column

Disclaimer: The information provided on DevExpress.com and affiliated web properties (including the DevExpress Support Center) is provided "as is" without warranty of any kind. Developer Express Inc disclaims all warranties, either express or implied, including the warranties of merchantability and fitness for a particular purpose. Please refer to the DevExpress.com Website Terms of Use for more information in this regard.

Confidential Information: Developer Express Inc does not wish to receive, will not act to procure, nor will it solicit, confidential or proprietary materials and information from you through the DevExpress Support Center or its web properties. Any and all materials or information divulged during chats, email communications, online discussions, Support Center tickets, or made available to Developer Express Inc in any manner will be deemed NOT to be confidential by Developer Express Inc. Please refer to the DevExpress.com Website Terms of Use for more information in this regard.