• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 407
  • Last Modified:

Problems inserting a bitmap into cells of an xStringGrid

Hello,

I am populating an xtstringgrid object with the contents of a kbmtable.  I have no problem getting the data into the grid however I'm running into problems loading bitmaps in.  If I add debug code to step through each row I can see the bitmap in the correct cell...until it comes time for the bitmap to go into a row that is hidden by scrolling.  The bitmap then gets drawn at the top of the grid.  When all rows are completed the focus of the table shifts to the right and all of the bitmaps disappear.  I have to highlight the grid and drag to the left to see the entire contents (again, missing the bitmaps).  Any insight as to what I'm missing here would be greatly appreciated.  I'm also trying to figure out how to add a button in a cell for each row but that might be best saved for another question.  Here's the code:

procedure TTSearchFrame.BuildTable(thisFL: TFareLadder);
var
  k: integer;
  tempField: TField;
  tempStr: string;
  Bitmap: TBitmap;
  thisRect: TRect;
begin
  thisFL.kbmMemTable1.First;
  k := 1;
  StringGrid1.RowCount := 2;
  StringGrid1.RowCount := thisFL.kbmMemTable1.RecordCount + 1;
  while not thisFL.kbmMemTable1.Eof do
  begin
    tempField := thisFL.kbmMemTable1.Fields.FieldByName('FareType');
    tempStr := tempField.Text + '.bmp';
    Bitmap := TBitmap.Create;
    Bitmap.LoadFromFile(tempStr);
    thisRect := StringGrid1.CellRect(0, k);
    StringGrid1.Canvas.Draw(thisRect.Left, thisRect.Top, Bitmap);
    Bitmap.Free;
    tempField := thisFL.kbmMemTable1.Fields.FieldByName('Price');
    StringGrid1.Rows[k].Strings[1] := tempField.text;
    tempField := thisFL.kbmMemTable1.Fields.FieldByName('Class');
    StringGrid1.Rows[k].Strings[2] := tempField.text;
    tempField := thisFL.kbmMemTable1.Fields.FieldByName('Company');
    StringGrid1.Rows[k].Strings[3] := tempField.text;
    tempField := thisFL.kbmMemTable1.Fields.FieldByName('Status');
    StringGrid1.Rows[k].Strings[4] := tempField.Text;
    tempField := thisFL.kbmMemTable1.Fields.FieldByName('FLID');
    StringGrid1.Rows[k].Strings[5] := tempField.Text;
    StatusBar1.SimpleText := 'Drawing the grid... (' + inttostr(k) + '/' + inttostr(thisFL.kbmMemTable1.RecordCount) + ')';
    thisFL.kbmMemTable1.Next;
    k := k + 1;
  end;
end;

Again, the table gets set up properly aside from the bitmap issue and the shifting of the table upon completion.  Thanks!
0
AEDeveloper
Asked:
AEDeveloper
  • 2
1 Solution
 
mokuleCommented:
You need to draw bitmap in OnDrawCell event
0
 
AEDeveloperAuthor Commented:
I determine which bitmap to put in the cell based on the text for that cell.  How do I access that information from the OnDrawCell method?

My OnDrawCell method would look something like this

procedure TTSearchFrame.StringGrid1DrawCell(Sender: TObject; ACol,
  ARow: Integer; Rect: TRect; State: TGridDrawState);
var
  Bitmap: TBitmap;
  thisRect: TRect;
  tempStr: string;
begin
  if (ACol = 1) then begin // I only want the bitmap in the first column cells
  tempStr := tempField.Text + '.bmp'; // that's the text I'd need to get from the cell, I know tempField doesn't exist here.
  Bitmap := TBitmap.Create;
  Bitmap.LoadFromFile(tempStr);
  thisRect := StringGrid1.CellRect(0, k);
  StringGrid1.Canvas.Draw(thisRect.Left, thisRect.Top, Bitmap);
  Bitmap.Free;
  end;
end;
0
 
AEDeveloperAuthor Commented:
disregard previous post, I figured it out.  You were right with the OnDrawCell.  Thanks!
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now