Cells and colors


i have a noobi question for something i didn't find the answer to:
is there a way to change the color of specific cells in a grid please?
(i can only change the color of complete rows and cols)


Easiest way is via:

grid.Colors[col,row]: TColor

alternatively, via OnGetCellColor (demo 2 shows this)

sorry Bruno, i should have said that i was talking about the intraweb TIWAdvWebGrid component.

ok, i found how to color each cell(in the OnGetCellProp), but something is not working for me.

i have this simple code :
  With TIWAdvWebGrid1 Do
    If (Color=clWebWHITE) AND (Font.Color=clWebBLACK) Then Begin
    Else Begin

when i add the code to the OnGetCellProp event, it changes the color of the cells i clicked to blue, but when i click the cell again, it doesn't change back to white.

and when i use that code in the OnAsyncCellClick, nothing happens.

What am i missing please?

The Color parameter in the OnGetCellProp event is the default cell color, not a color you might have set previously via this event, hence, the code snippet you provide won't work.

Not sure what exactly you want to achieve. If you want to set selected cells in a specific colors, use the selection capabilities of the grid and you can set the selection colors separately via grid properties.

thanks Bruno,.

the selectionis not good for what i need.
what i want exactly is to set a cell's color(and font color) to any color i want, every time i click a cell, like an On/Off "toggle" for cells.

for example :
base grid's colors are :
font : black
background : white.

when i click Cell[2, 3], i want to change it to :
font : yellow
background : blue (or green, or what ever) - but only the cell i clicked

then i want to change another cell - Cell[4, 7], i want to change this one too to :
font : yellow

background : blue (or green, or what ever)

now i want to "turn off" the first cell so the colors must change back to font-black, and back-white.

just a simple change-the-cell's-color thing.

sorry if this is too simple to ask, but i couldn't find anything that can help me do this.


to be clearer, i'm talking about the TIWAdvWebGrid , from the TMS intraweb pack

Then you'll need to store the on-off state of the cells somehow and use this state to determine the color to apply from the OnGetCellProp event.

so there is nothing i can use to get to [x, y] and just change the color?
maybe you can add this in a future version?

here is what i tried and it's still doesn't work :
in the private of the form :
  Toggle : array [1..2, 1..33] of Boolean;
procedure TIWForm1.TIWAdvWebGrid1AsyncCellClick(Sender: TObject; EventParams: TStringList; RowIndex, ColumnIndex: Integer);
  Toggle[RowIndex, ColumnIndex]:=NOT Toggle[RowIndex, ColumnIndex];
procedure TIWForm1.TIWAdvWebGrid1GetCellProp(Sender: TObject; RowIndex,
  ColumnIndex: Integer; AValue: string; var AColor: TIWColor;
  var AAlignment: TAlignment; Font: TIWFont);
  Case Toggle[RowIndex, ColumnIndex] Of
    True  : Begin
    False : Begin

this still doesn't color the cells.
if i understand it correctly, the OnGetCellProp trigers only after the OnCellClick, right?
what did i miss please?
can you show me with some code how it can be done please?


OnGetCellColor is triggered upon a grid refresh, when the grid is fully rendered.
So, when you want to update a color, you'll need to trigger a full grid re-render.

so we can't do it the Async way...?
i didn't find an "OnGetCellColor" event (in TIWAdvWebGrid?) .

i tried the same code again, but this time from the OnCellClick.
now it worked fine, except it didn't change the background color of the cell, only the Font color.


ok, i finally got it to work.
i should have use AColor instead of Color (and to use ColumnIndex+1 and RowIndex+1...) .

too bad we can't do it Async...

Thank you Bruno

ok i was wrong again (or only half right...) .

clicking and changing the colors of the clicked cell is working fine, but i still can't change the color of other cells.
i managed to make the "toggle" work, but that is only 1 part of what i need.

i need to be able to change the color of 2 cells i click, and also to change the color of all the cells between the 2 cells i clicked.
so if i click Cell[1, 1], and also Cell[1, 10] then i need all the cells from cell[1,2] to cell[1, 9] to change their color programmaticaly.
that way, i will have all the cells from [1, 1] to [1, 10] in blue (or any other color).

any idea how to make this please?

thanks (and i'm sorry for my broken english...)

You'd need to store that for other cells the colors need to change too and use these values to steer color rendering from the OnGetCellProp event.

hi bruno,.

i already tried that, but for some reason, i can't make it to work.
the "steer color rendering from the OnGetCellProp event" is the part that doesn't work for me.
can you show me an example code, like coloring 3 different random cells please?