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
199 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…
Suggested Courses
Course of the Month10 days, 18 hours left to enroll

628 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