delphi : how can I change color of a cell in string grid
Asked Answered
L

3

14

I want to change background color ( not font ) of a cell in string grid in delphi .

Just one cell not a row or a column.

Can I?


RRUZ : your procedure is correct and works but in my procedure doesn't work.

My procedure:

x is a global array of integer

procedure TF_avalie_salon.StringGrid1DrawCell(Sender: TObject; ACol,
    ARow: Integer; Rect: TRect; State: TGridDrawState);
    var   S: string;
begin
    S := StringGrid1.Cells[ACol, ARow];
    StringGrid1.Canvas.FillRect(Rect);
    SetTextAlign(StringGrid1.Canvas.Handle, TA_CENTER);
    StringGrid1.Canvas.TextRect(Rect,Rect.Left + (Rect.Right - Rect.Left) div 2, Rect.Top + 2, S);
    if (ARow<>0 )AND(acol<>0)AND(gridclick=true) then
    begin
        try
          gridclick:=false;
          x[acol+((strtoint(Edit_hafte.Text)-1)*7),arow]:=strtoint(StringGrid1.Cells[ACol, ARow]);
        except
          x[acol+((strtoint(Edit_hafte.Text)-1)*7),arow]:=0;
          StringGrid1.Cells[acol,arow]:='0';
          with TStringGrid(Sender) do
          begin
            Canvas.Brush.Color := clGreen;
            Canvas.FillRect(Rect);
            Canvas.TextOut(Rect.Left+2,Rect.Top+2,Cells[ACol, ARow]);
          end;
        end;
    end;
end;

When I use Canvas.Brush.Color with below code , Canvas.Brush.Color doesn't work. If I inactive below code I can change the cells color. But I need both.

    S := StringGrid1.Cells[ACol, ARow];
    StringGrid1.Canvas.FillRect(Rect);
    SetTextAlign(StringGrid1.Canvas.Handle, TA_CENTER);
    StringGrid1.Canvas.TextRect(Rect,Rect.Left + (Rect.Right - Rect.Left) div 2, Rect.Top + 2, S);
Langlois answered 15/7, 2011 at 0:20 Comment(4)
The code which makes the cell Green is only executed in case of an exception and that is almost only possible in the StrToInt-function. Is that intentional?Quadrivalent
yeah andreas ,,, i want to change a cell color when it makes a problem .Langlois
admin or modirator : can i ask this question again ( for completing and clearing )Langlois
This is duplicated, there are many questions reggarding customdraw here in stackoverflow. Painting a cell or a text works the same way. You just need to use the correct property.Almshouse
S
13

The Rafael link contains all which you need, using the OnDrawCell event is the way to paint the cells of a StrignGrid. check this sample which paint only the background of an specific cell.

procedure TForm1.StringGrid1DrawCell(Sender: TObject; ACol, ARow: Integer;  Rect: TRect; State: TGridDrawState);
begin
  if (ACol = 3) and (ARow = 2) then
    with TStringGrid(Sender) do
    begin
      //paint the background Green
      Canvas.Brush.Color := clGreen;
      Canvas.FillRect(Rect);
      Canvas.TextOut(Rect.Left+2,Rect.Top+2,Cells[ACol, ARow]);
    end;
end;
Skeg answered 15/7, 2011 at 0:51 Comment(4)
thank you ,,, it works aloan but not in my procedure . please check my procedure in the body of my question . thank you .Langlois
What do you mean which not work? are you tried setting a brekpoint in the line where your set the background color (Canvas.Brush.Color := clGreen;) to check if the application reach that point?Skeg
RUUZ : yeah i make a breakpoint , its reachable . i think i cant use it because your code is correct and its from application . i don't know what to do .Langlois
its not about my think or your think . its a simple code in less than 15 lines that i said not works together but both of them was works right alone . now if u can help its so nice and i will so Grateful from you but other comments that not help are spam .Langlois
R
4

I used these codes, translated to C++. There are two specific notes, then I'll post the code.

  1. In "StringGrid1", the property "DefaultDrawing" must be FALSE for this to work.

  2. The "Canvas" object must be fully qualified: ie. StringGrid1->Canvas->Font->Color =clBlack.

CODE:

void __fastcall TForm3::StringGrid1DrawCell(TObject *Sender, int ACol, int ARow, TRect &Rect,
      TGridDrawState State)
{
UnicodeString   uStr = "Hello";
int     k, l;
char    cc[100];


if(TRUE)
    {
    if((ACol <= 1) || (ARow <= 1))
        {
        StringGrid1->Canvas->Font->Color = clBlack;
        StringGrid1->Canvas->Brush->Color = clBtnFace;
        if(ACol == 0)
            {
            if(ARow > 1)
                {
                sprintf( cc, " %5.1f", rowLabels[ARow - 2]);
                uStr = cc;
                StringGrid1->Canvas->TextRect( Rect, Rect.left+2, Rect.top+2, uStr);
                StringGrid1->Canvas->FrameRect(Rect);
                }
            }
        if(ARow == 0)
            {
            if(ACol > 1)
                {
                sprintf( cc, " %5.1f", colLabels[ACol - 2]);
                uStr = cc;
                StringGrid1->Canvas->TextRect( Rect, Rect.left+2, Rect.top+2, uStr);
                StringGrid1->Canvas->FrameRect(Rect);
                }
            }
        }
    else
        {
        switch (ACol%2)
            {
            case 0:
                {
                StringGrid1->Canvas->Font->Color = clRed;
                StringGrid1->Canvas->Brush->Color = 0x00E1FFF9;
                break;
                }
            case 1:
                {
                StringGrid1->Canvas->Font->Color = clBlue;
                StringGrid1->Canvas->Brush->Color = 0x00FFEBDF;
                break;
                }
            }
        StringGrid1->Canvas->TextRect( Rect, Rect.left+2, Rect.top+2, uStr);
        StringGrid1->Canvas->FrameRect(Rect);
        }
    }
}
Rhizobium answered 26/9, 2012 at 3:40 Comment(0)
M
-1
procedure StringGrid1DrawCell(Sender: TObject; ACol, ARow: Integer;
  Rect: TRect; State: TGridDrawState);
var
i:integer;  
begin
  with Sender as TStringGrid do
    begin
        Canvas.FillRect(Rect);
        DrawText (Canvas.Handle,
            PChar(Cells[ACol, ARow]),
            Length(Cells[ACol, ARow]),
            Rect, DT_WORDBREAK or DT_EXPANDTABS or DT_CENTER);
    end;
    for i:=2 to  StringGrid1.RowCount - 1 do
    if StringGrid1.Cells[3,i]='' then
      begin
        StringGrid1.Canvas.Brush.Color:=clRed;
          if ((ACol=3)and(ARow=i)) then
            begin
              StringGrid1.Canvas.FillRect(Rect);

            end;
      end;


end;
Moody answered 11/3, 2018 at 18:36 Comment(9)
Why do you use the Sender as TStringGrid cast as well as the direct reference to StringGrid1. And are you intending that this should be a free-standing procedure and not a method of the form containing StringGrid1 and if free-standing, how do you envisage it would actually be called?Lyly
Perhaps you might want to explain why you think this code works and/or what was missing or incorrect in the original post...Selaginella
The first part of the code sets the color of the cell, and also aligns the text to the center. The third part of the code sets the color of a certain cell when the condition is met. In this code, the condition for changing the color of the cell is to check for an empty value, if it is "empty" in the cell, then it is highlighted with a red cet.Moody
these procedures were specially written for client use. If the user does not enter the data correctly or does not enter anything at all, the cell changes color to red. This section of the code checks for empty values, the last cell in the column.Moody
Sender as StringGrid is used because this procedure inherits dynamic StringGrid, when generating reports using fastreportMoody
Imo your code is an ill-conceived mess. I suggest you delete it because it will be of zero value to future readers (which is the point of SO)Lyly
Perhaps the code was written quickly for the task, for the "beauty" of the code it did not go, after writing the code did not addMoody
the task was to align the text to the center, and check for null value in the cellMoody
Thank you for the answer, it was helpful to me. I'm using C++ so for me all Delphi code looks like a mess anyway :)Stanton

© 2022 - 2024 — McMap. All rights reserved.