Icon View Thread

The following is the text of the current message along with any replies.
Messages 1 to 9 of 9 total
Thread Row colouring example
Thu, Jun 27 2019 12:17 PMPermanent Link

Matthew Jones

I can't find an example of how to colour the rows of a grid according to some aspect in the data for that row. Is there such?

Is it safe to assume that the grid's attached DataSet is currently on the drawn row?

If it isn't simple, will this be easy in EWB3?

--

Matthew Jones
Thu, Jun 27 2019 1:30 PMPermanent Link

Raul

Team Elevate Team Elevate

On 6/27/2019 12:17 PM, Matthew Jones wrote:
> I can't find an example of how to colour the rows of a grid according to some aspect in the data for that row. Is there such?


Yes and it's pretty simple - you have to handle OnCellUpdate event and
then you can use the cell Data property to see what data is actually
present.

For example something like :

procedure TMainForm.GridColumn1CellUpdate(Sender: TObject; ACell:
TGridCell);
begin
   if ACell.Data ='0' then
ACell.Background.Fill.Color:=clElevateLightGreen;
end;

Search NG for OnCellUpdate and there should be lot of references

> Is it safe to assume that the grid's attached DataSet is currently on the drawn row?

Not sure but i'd rely on OnCellUpdate and Cell Data property since grid
itself could be redrawn for various reasons not related to dataset
navigation.

You can also use Acell.Index to know which column you're on (if you have
same event for all of them) and also Grid.ROwIndex to know which row
you're on


Raul





Fri, Jun 28 2019 3:50 AMPermanent Link

Matthew Jones

Raul wrote:

>   if ACell.Data ='0' then ACell.Background.Fill.Color:=clElevateLightGreen;

But that can't look at the third cell's data to set the whole row to green. That's the need, not just one cell.

I guess I can use the single cell for now, but I want the whole row coloured (or left default).

--

Matthew Jones
Fri, Jun 28 2019 4:36 AMPermanent Link

Huseyin Aliz

myBiss ApS

Avatar

Hi Matthew,

I am using following:

procedure TBilListeForm.SetupCells(ACell: TGridCell);
begin
     if (ACell.index mod 2=0) and
(SameText(ACell.InterfaceState,NORMAL_STATE_NAME)) and (ACell.Index <>
BilerGrid.RowIndex) then begin
     ACell.Font.Size := 12;
     ACell.Font.Color := clBlack;
     ACell.Background.Fill.Color := clWhite;
     end
     else if (ACell.Index <> BilerGrid.RowIndex) and
(SameText(ACell.InterfaceState,NORMAL_STATE_NAME)) then
     begin
     ACell.Font.Size := 12;
     ACell.Font.Color := clBlack;
     ACell.Background.Fill.Color:=clWhiteSmoke;
     end;
     if (ACell.Index = BilerGrid.RowIndex) and
(SameText(ACell.InterfaceState,BOTH_STATE_NAME)) then begin
     ACell.Font.Size := 12;
     ACell.Font.Color := clWhite;
     ACell.Background.Fill.Color := clRed;
     end;
end;

procedure TBilListeForm.KontonrCellUpdate(Sender: TObject; ACell:
TGridCell);
begin
SetupCells(ACell);
end;

procedure TBilListeForm.RegnrCellUpdate(Sender: TObject; ACell: TGridCell);
begin
SetupCells(ACell);
end;

etc...

Hope it's something you can use Smile

Regards,
Hüseyin



Den 28-06-2019 kl. 09:50 skrev Matthew Jones:
> Raul wrote:
>
>>    if ACell.Data ='0' then ACell.Background.Fill.Color:=clElevateLightGreen;
> But that can't look at the third cell's data to set the whole row to green. That's the need, not just one cell.
>
> I guess I can use the single cell for now, but I want the whole row coloured (or left default).
>
Fri, Jun 28 2019 4:42 AMPermanent Link

Matthew Jones

Thanks, but I can't see where it is getting the colour state from the data in the dataset? For colouring each row alternately, it would work nicely, but I want it to reflect a status in the data row. So an error state shows the whole line red, but others are normal.



--

Matthew Jones
Sat, Jun 29 2019 10:55 AMPermanent Link

Raul

Team Elevate Team Elevate

On 6/28/2019 3:50 AM, Matthew Jones wrote:
> Raul wrote:
>
>>    if ACell.Data ='0' then ACell.Background.Fill.Color:=clElevateLightGreen;
>
> But that can't look at the third cell's data to set the whole row to green. That's the need, not just one cell.
>
> I guess I can use the single cell for now, but I want the whole row coloured (or left default).


I did run some tests and it does look like the dataset is being also
navigated as grid is updated so you could try it and see if you have any
issues (only issue i can think of is if grid OnCellUpdate is trigerred
without dataset cursor change)

so something like this in the OnCellUpdate

if MyBoundDataset.Columns['ValueICareAbout'].AsString = '0' then
ACell.Background.Fill.Color:=clElevateLightGreen;



Raul
Thu, Jul 4 2019 10:58 AMPermanent Link

Richard Harding

Wise Nutrition Coaching

Matthew,

I asked Tim a similar question back in November 2015.

https://www.elevatesoft.com/forums?action=view&category=ewb&id=ewb_general&page=1&msg=8544#8544

The example below may be helpful. The customer is an engineering company and the DB manages the stock. Below is a view with some basic stock information.

Whilst if is not possible to highlight a row, the following was sufficient for the customer which may be relevant in your case.

CREATE VIEW "ADRSubstitute" AS
SELECT
   StockNumber, Category, Number, Identity, Description
 FROM Stock
 WHERE Category = 'ADR'
 ORDER BY Category, Identity, Number

StockNumber is the primary key formatted as XXX/<n> where XXX is the category.

Each stock item can be sourced from a number of suppliers with different product codes and therefore different StockNumbers. "Identifier" is a code that is that the same for items that can be substituted. The customer needs to highlight the row that contained the first item where the identity changed. In the example below, ADR/10, ADR/2, ADR/9, ADR/17 needs to be highlighted.

"StockNumber","Category","Number","Identity","Description"
"ADR/10","ADR",10,"15X3T               ","135MM..IDLER WITH SPRING & GREASE ADJ.."
"ADR/11","ADR",11,"15X3T               ","135MM X 39 LINK X 450 or 300 TB TRACK ASSY"
"ADR/12","ADR",12,"15X3T               ","BOTTOM  ROLLERS  SF"
"ADR/2","ADR",2,"T750                ","IDLER ASSY INCLS  SPRING & GREASE ADJ..<not use ..see ADR 3>"
"ADR/3","ADR",3,"T750                ","IDLER ASSY INCLS SPRING & GREASE ADJ..IDL214"
"ADR/4","ADR",4,"T750                ","101MM PITCH..TRACK GROUP..ALSO TAS 10..< cut to 250mm>"
"ADR/16","ADR",16,"T750                ","27 x Link sect ..250mm TB Shoes"
"ADR/9","ADR",9,"UB101Z0N42  250     ","TRACK..ADR9..TAS48 ..@ 42 LINK..250TB .. TRACK ASSY."
"ADR/17","ADR",17,"UR101K123           ","ADS..MOD"

It is possible to create a view based on the Stock table with sufficient data to determine the first row for each identity. Creating an CellUpdate event for the StockNumber cell allowed the StockNumber cell to be highlighted which is sufficient in this case.

For example,

procedure TfmADRStock.grStockNumberCellUpdate(Sender: TObject; ACell: TGridCell);
var
  s:        array of string;
  i1, i2:   integer;

begin
  esADRHeaders.InitFind;                           // view to determine when the change of identity occurs
  esADRHeaders.Columns['StockNumber'].AsString := ACell.Data;
  esADRHeaders.Find;

  if length(ACell.Data) > 0 then
  begin
    s := split(ACell.Data, '/');
    i1 := esADRHeaders.Columns['H1'].AsInteger;    // i1 is the stock number of the row where the change of identity occurs (the header row)
    i2 := StrToInt(s[1]);                          // i2 stock number of currenr row
    ACell.Font.Style.Bold := (i1 = i2);
  end;
end;


You could also change the background and font color.

Richard
Fri, Jul 5 2019 3:53 AMPermanent Link

Matthew Jones

Thanks - so still just a single cell? Strikes me that if it were possible to know/control the order of the drawing, you could use the first column and set a global to use in the subsequent columns of a row.

It isn't critical for me in this case, but I hope that this is answered in EWB3 - some way to access the current data row for the cell as it is updated/drawn.


--

Matthew Jones
Fri, Jul 5 2019 8:09 AMPermanent Link

Richard Harding

Wise Nutrition Coaching

Matthew

Yes - it is still a single cell. The OnCellUpdate event is called many, many times.

A couple points in case you wish to try it out.

Firstly, the first group of OnCellUpdate events does not have ACell.Data defined so you need to check that ACellData is assigned.

In the AfterLoad event for the lookup dataset, you need to add Grid1.DataSet.First. This will force another set of OnCellUpdate events that will perform the formatting. Otherwise, the cells will not be formatted until you scroll the items in the grid.

Richard



Attachments: ColoredGrid.jpg
Image