Solved

How to do a two dimension grid ? How to color the row and column of a grid when focused or selected ?

Posted on 2002-06-11
12
195 Views
Last Modified: 2010-04-04
I have a query on how to come out with a grid like this :-

Year     OFFICE1     OFFICE2     OFFICE3
2000     1000        1000        3000
1999     1000        1100        2000
1998     800         2000        1500

Then how to color them if I click on cells with value "3000" ?  I need to color them in this way where the row of year 2000 is colored and colum of Office3 is colored as well.

Please guide.
0
Comment
Question by:ivylnm
  • 4
  • 4
  • 2
  • +2
12 Comments
 
LVL 1

Expert Comment

by:MBo
ID: 7071876
set
DBGrid1.DefaltDrawing:= False;  

This is an example for TTable to show the principle.

procedure TForm1.DBGrid1DrawColumnCell(Sender: TObject; const Rect: TRect;
  DataCol: Integer; Column: TColumn; State: TGridDrawState);
begin
if Column.Field.DisplayName='LAST_NAME' then
    DBGrid1.Canvas.Brush.Color := clBlue;
if Table1.FieldByName('ACCT_NBR').AsInteger =2094056 then
    DBGrid1.Canvas.Font.Color := clLime;
  DBGrid1.DefaultDrawColumnCell(Rect,DataCol, Column, State);
end;
0
 

Author Comment

by:ivylnm
ID: 7074763
Thanks for guiding.

But the codes that you provide is useful only if I want the data to be displayed in different colors.  Now my requirement is when I click on the grid where the data has been loaded into the grid, and that row and column of that cell are colored maybe with blue color.
0
 
LVL 25

Expert Comment

by:Tomas Helgi Johannsson
ID: 7075173
Hi!

I think this is what you are looking for.

procedure TForm1.DBGrid1DrawColumnCell(Sender: TObject; const Rect: TRect;
  DataCol: Integer; Column: TColumn; State: TGridDrawState);
begin
   if gdSelected in State then
   begin
        DBGrid1.Canvas.Brush.Color := clRed;
        DBGrid1.DefaultDrawColumnCell(Rect,DataCol, Column, State);
   end
   else
   begin
        if Column.Field.DisplayName='NAME' then
        DBGrid1.Canvas.Brush.Color := clBlue;
        if Table1.FieldByName('NAME').AsString ='Boa' then
        DBGrid1.Canvas.Font.Color := clLime;
        DBGrid1.DefaultDrawColumnCell(Rect,DataCol, Column, State);
   end;
end;

Regards,
  Tomas Helgi

0
Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
LVL 4

Accepted Solution

by:
nestorua earned 200 total points
ID: 7076285
HI,
You can do it the following way:
//
var ToHiglight: boolean;
         Value: integer;
//
procedure TForm1.DBGrid1CellClick(Column: TColumn);
begin
  if Column.FieldName='Area'
   then
    begin
     ToHiglight:=True;
     Value:=Column.Field.AsInteger;
    end
   else ToHiglight:=False;
//
 DBGrid1.Invalidate;
end;
//
procedure TForm1.DBGrid1DrawColumnCell(Sender: TObject; const Rect: TRect;
  DataCol: Integer; Column: TColumn; State: TGridDrawState);
begin
 with DBGrid1 do
  begin
   if ToHiglight
    then
     begin
      if (((Column.FieldName='Area') or
          (DataSource.DataSet.FieldByName'Area').AsInteger=
                     Value))
            and not (gdSelected in State))
       then
        with Canvas do
         begin
          Brush.Color:=clBlue;
           Font.Color:=clWhite;
         end;
       end;
//
    DefaultDrawColumnCell(Rect, DataCol, Column, State);
//
   end;
end;
I try that on DBDEMOS database (Country table).
Don't forget to set DefaultDrawing=True (the property of the TDBGrid).
Sincerely,
Nestorua.
0
 

Author Comment

by:ivylnm
ID: 7077489
Thanks for the guide.  But this only solve part of my question.  

  How to color that specific column on the run time ? We can easily color a column at design time.  But I tried to color a column at run time but failed.

Besides, any idea of what kind of grid can allow to do two dimensional grid as shown above ?
0
 
LVL 4

Expert Comment

by:nestorua
ID: 7079172
HI,
I don't understand you. My code DOES make what you want in RUNTIME not designtime.
You didn't test it I guess.
Sincerely,
Nestorua.
0
 

Author Comment

by:ivylnm
ID: 7083265
Hi,  the codes does color a cell in RUNTIME, but I am actually looking for codes that can color a row of data and a column of data.

Such as :-
Year     OFFICE1     OFFICE2     OFFICE3
2000     1000        1000        3000
1999     1000        1100        2000
1998     800         2000        1500

If I click on cells value "1500", then it shall highlight the row of 1998 and column of OFFICE3.

Thanks,
Ivy
0
 
LVL 4

Expert Comment

by:nestorua
ID: 7083322
HI,
Still I don't understand you.
Try my code, that's all.
It works and do just what you want - highlight the corresponding row and column.
Sincerely,
Nestorua.
0
 
LVL 4

Expert Comment

by:nestorua
ID: 7083327
HI,
Still I don't understand you.
Try my code, that's all.
It works and does just what you want - highlights the corresponding row and column.
Sincerely,
Nestorua.
0
 

Author Comment

by:ivylnm
ID: 7123947
Sorry, I will try your codes later on as I get my hands on other projects these days.  Thanks for the guidance, shall reply you after I have tried the codes.
0
 
LVL 1

Expert Comment

by:pnh73
ID: 9010657
No comment has been added lately, so it's time to clean up this TA.
I will leave a recommendation in the Cleanup topic area that this question is:

Accept answer from nestorua

Please leave any comments here within the next seven days.
 
PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!
 
Paul (pnh73)
EE Cleanup Volunteer
0
 
LVL 1

Expert Comment

by:pnh73
ID: 9017620
Thankyou for your response.

Paul (pnh73)
EE Cleanup Volunteer
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Delphi cmd execution 6 67
Tviruailstringtree sort multi columns on header click 1 74
firemonkey keyboard covers the controls 1 37
Firemonkey allowing RTL on android 6 47
Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

856 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question