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

Asked

Viewed 1,918 times

4

I’m doing a expense control system, and when registering a new revenue or new expense I want you to change the line color of that record on dbGrid as chosen, I used a TDBRadioGroup to choose between 'Expense' and 'Recipe'. What the code would look like?

  • would be a color for expense and another for revenue?

  • Yes, as I decided to record the expenses and revenues in the same table, I want to differentiate in dbGrid

3 answers

3


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

Two clicks on the event onDrawColumnCell 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 to check if in the TYPE field of your table is 'expense', paint red, or paint green. But you can choose the color you want.

In the example I quoted, I made a condition using a string to facilitate, but recommend using a field integer 0 for expenditure and 1 for revenue.

2

in the 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);
  • 1

    I started to participate today in the community, I still do not master the usability of the site including formatting, but thanks for the scolding....

  • Do not take it as a scolding, and sorry if I passed that impression. But the formatting here on Sopt is fundamental... there are people who give ugly crap.. I hug and hope to help you on other occasions.

  • Thanks again, we’re together....

0

At the 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 instead of changing the line color, just change the font color, change the line:

DBGrid1.Canvas.Brush.Color := clBlue;  

for

DBGrid1.Canvas.Font.Color := clBlue;  

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

Browser other questions tagged

You are not signed in. Login or sign up in order to post.