How to change the color of the line of a DBGrid in Delphi?

4

I am doing a cost control system, and when I register a new revenue or new expense I want to change the color of the line of this record in dbGrid as chosen, I used a TDBRadioGroup to choose between 'Expense ' and ' Revenue '. What would the code look like?

    
asked by anonymous 25.11.2016 / 02:48

3 answers

3

In your database you must have a field where you tell whether the value is an expense or revenue. Let's say the name of this field is called 'type' . You can change the colors like this:

Two clicks on the onDrawColumnCell event of your DBGrid

    If tabelaTIPO.Value = 'despesa' then // condição
      begin
      Dbgrid1.Canvas.Font.Color:= clRed; // coloque aqui a cor desejada
      Dbgrid1.DefaultDrawDataCell(Rect, dbgrid1.columns[datacol].field, State);
      end else
      begin
      Dbgrid1.Canvas.Font.Color:= clGreen; // coloque aqui a cor desejada
      Dbgrid1.DefaultDrawDataCell(Rect, dbgrid1.columns[datacol].field, State);
      end;

What the code does is check if in the TYPE field of your table it is 'expense', it looks red, otherwise it looks green. But you can choose the color you want.

In the example I mentioned, I made a condition using a string to make it easier, but I recommend using an integer 0 field for expense and 1 for revenue.     

25.11.2016 / 03:10
2

no OnDrawColumnCell of DBGrid :

    if (DBRadioGroup1.ItemIndex = 0) then  
      DBGrid1.Canvas.Font.Color := clRed  
    else  
      DBGrid1.Canvas.Font.Color := clBlue;


DBGrid1.DefaultDrawColumnCell(Rect, DataCol, Column, State);
    
25.11.2016 / 19:06
0

In event onDrawColumnCell of DBGrid you must include the code:

if DBRadioGroup1.ItemIndex = 0 then
begin
  DBGrid1.Canvas.Brush.Color := clBlue;
  DBGrid1.DefaultDrawDataCell(Rect, Column.Field, State);
end
else
begin
  DBGrid1.Canvas.Brush.Color := clRed;
  DBGrid1.DefaultDrawDataCell(Rect, Column.Field, State);
end;

If you want to change the color of the line, just change the font color, change the line:

DBGrid1.Canvas.Brush.Color := clBlue;  

by

DBGrid1.Canvas.Font.Color := clBlue;  

or even combine the two lines, changing both the background and the font.

    
25.11.2016 / 11:31