简体   繁体   中英

Delphi add image in front of Row for DBGrid

When I get an error on the OnDataChange event for my DataSource, i need to add un image Alert in front of Row.

在此处输入图片说明

How we can do that?

I've the following starting point: http://delphiexamples.com/databases/gridimages.html

Here's my suggestion for a fix.
In the datachange mark the field with an errorcode if needed using the tag property.
Come display time check the field tag and display a bitmap if needed.
I recommend storing the bitmaps in a ImageList; don't get the bitmaps from the DB it will hammer your database for no reason.

procedure TForm1.DataSource1DataChange(Sender: TObject; Field: TField);
begin
  if SomeCondition then Field.Tag:= Field.DataSet.RecNo
  else Field.Tag:= 0;
end;

If you can have multiple rows with errors, then you'll have to use an external list of rows with errors. A TList<integer> will work just fine for that purpose.
In the draw method check to see if the current row is in the error list.

Delphi's default DBGrid does not facilitate drawing column and row headers.
However it also does not forbid it, so you're going to color outside the lines.

procedure TForm1.DBGrid1DrawDataCell(Sender: TObject; const Rect: TRect;
  Field: TField; State: TGridDrawState);
var
  index: integer;
begin
  //Only draw one error per field.  
  //you might also want draw the error indicator in the field itself. 
  //and not in the row header.
  if (Field.Tag = Field.Dataset.RecNo) and (Field = Table1Field1) then begin
    //never mind the Rect we can draw where we like.
    index:= 1;
    ImageList1.Draw(DBGrdi1.Canvas, 2, Rect.Top, index, dsTransparent);  
  end
  else DBGrid1.DefaultDrawDataCell(Rect, Field, State);
end;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM