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?
Delphiprocedure 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.
Delphiprocedure 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