Stefano Losi
Stefano Losi

Reputation: 729

Set row color in TLMDGrid

I'm using LMD Innovative library in my inherited Delphi project, and in particular the TLMDGrid component. I just want to set last row color (summary) different from the rest of the table. I can set different colors for different columns in the designer, but (due to poor documentation) cannot find how to set color for a single row. Thanks for help.

Upvotes: 0

Views: 251

Answers (1)

MartynA
MartynA

Reputation: 30715

I don't use the LMD grids myself, so the following is based on their most recent trial download.

I wasn't sure whether you were asking about the TLMDGrid or the TLMDDbGrid so the minimal demo below shows how to set the last row of both of them to a specific color. As you'll see, it is just a question of setting up an OnGetCellColor event handler for each grid, and then setting the value of the AColor variable by whatever criteria suit you.

The onGetCellColor event is passed the current column as well as the row number of the grid cell which is about to be drawn, so this show give you the possibility of coloring cells in the same row differently if you want.

I confess I'm not very happy with basing the test on the OnGetCellColor event of LMDDBGrid1 on the dataset's RecordCount, because not all dataset types return a meaningful value (and for some, there can be big performance hit in getting its value. With a standard TDBGrid, in its drawing events, you can rely on the dataset cursor being synced with the event calls (so that any data values can be picked up from the current dataset row). I'm not sure yet about how you would do that with the LMD grid - obviously the best place to ask about that would be LMD themselves.

Code:

unit LMDGridTestu;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  DB, DBClient, Grids, LMDGrid, LMDDBGrid;

type
  TForm1 = class(TForm)
    CDS1: TClientDataSet;
    DataSource1: TDataSource;
    CDS1ID: TIntegerField;
    CDS1Name: TStringField;
    LMDDBGrid1: TLMDDBGrid;
    LMDGridIntegerColumn1: TLMDGridIntegerColumn;
    LMDGridTextColumn1: TLMDGridTextColumn;
    LMDGrid1: TLMDGrid;
    NameCol: TLMDGridTextColumn;
    IDCol: TLMDGridIntegerColumn;
    procedure FormCreate(Sender: TObject);
    procedure LMDDBGrid1GetCellColor(Sender: TObject; ACellState:
        TLMDGridCellStates; ARowState: TLMDGridRowState; const AData: Variant;
        AColumn: TLMDGridColumn; ARow: Integer; var AColor: TColor);
    procedure LMDGrid1GetCellColor(Sender: TObject; ACellState: TLMDGridCellStates;
        ARowState: TLMDGridRowState; const AData: Variant; AColumn: TLMDGridColumn;
        ARow: Integer; var AColor: TColor);
  public
  end;

[...]

procedure TForm1.LMDDBGrid1GetCellColor(Sender: TObject; ACellState:
    TLMDGridCellStates; ARowState: TLMDGridRowState; const AData: Variant;
    AColumn: TLMDGridColumn; ARow: Integer; var AColor: TColor);
begin
  if ARow = CDS1.Recordcount - 1 then
    AColor := clYellow;
end;

procedure TForm1.LMDGrid1GetCellColor(Sender: TObject; ACellState:
    TLMDGridCellStates; ARowState: TLMDGridRowState; const AData: Variant;
    AColumn: TLMDGridColumn; ARow: Integer; var AColor: TColor);
begin
  if ARow = LMDGrid1.DataRowCount - 1 then  //  Rows are numbered from zero
    AColor := clYellow;
end;

procedure TForm1.FormCreate(Sender: TObject);
var
  i : integer;
begin
  CDS1.CreateDataSet;
  CDS1.IndexFieldNames := 'ID';
  for i := 1 to 12 do
    CDS1.InsertRecord([i, StringOfChar(Chr((ord('a') + i -1)),  20)]);
  CDS1.First;

  LMDGrid1.DataRowCount := 10;

  for i := 0 to LMDGrid1.DataRowCount - 1 do begin
    LMDGrid1.Cells[IDCol.Position, i] := IntToStr(i);
    LMDGrid1.Cells[NameCol.Position, i] := 'Name' + IntToStr(i);
  end;

end;

Upvotes: 1

Related Questions